08-27-2010 04:25 AM
Hi,
I want to write text to a file in SDCard. The code I had written is below:
public EditField editfield;
public String filepath="file:///SDCard/BlackBerry/documents/Test
catch(IOException ioe)
{
}
When I debug the code the file got created;but when i entered some text data in simulator the data is not written in the file.
Can anyone help me on this.
If possible i want sample code to read data from the same file.
Solved! Go to Solution.
08-27-2010 06:43 AM
Did you setup the simulator to emulate an SD card?
08-27-2010 06:59 AM
When ur code will run at that time ur EditField is Blank. So no data are in the file.
Postur full code.
--------------------------------------------------
Press Kudoes and Accept as solution Button, If u got Answer.
Thanks.
08-27-2010 07:23 AM
Yes.
In the Memory of simulator i had specified the path.
Regards,
Pavan Kumar
08-27-2010 07:28 AM
Hi!!
Thanks for Reply.
i'm providing the entire code here.
public FileAppMainScreen()
{
// TODO Auto-generated constructor stub
editfield=new EditField("Enter Text:","");
add(editfield);
try
{
FileConnection fconn=(FileConnection)Connector.open(filepath,Conn
if(!fconn.exists())
{
fconn.create();
}
OutputStream os=fconn.openOutputStream();
os.write(editfield.getText().getBytes());
os.write("testcontentWelcome".getBytes());
//os.close();
}
catch(IOException ioe)
{
}
}
08-27-2010 07:42 AM
first the data put in the byte array. then try to write that byte array in the file.
--------------------------------------------------
Press Kudoes and Accept as solution Button, If u got Answer.
Thanks.
08-27-2010 09:03 AM
I created array of type byte; assigned array to get data from edit field and tried to write the array contents to the file.
No debugging errors but data is not writing to the file.
Thank U
08-29-2010 05:15 AM
This works for me:
public String write(String[] textArr,int lengthArr)
{
if(checkCard()<2) return("NOK No SDCARD");
String fileUrl = "file:///SDCard/LogFile.txt";
try
{
FileConnection fconn = (FileConnection)Connector.open( fileUrl);
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (!fconn.exists())
fconn.create(); // create the file if it doesn't exist
else
{
fconn.delete(); //Create a new one
fconn.create();
}
OutputStream os =fconn.openOutputStream();
String record;
for(int i=0;i<lengthArr;i++)
{
if(textArr[i].endsWith("\n")==false)
record = textArr[i]+"\n";
else
record = textArr[i];
os.write(record.getBytes());
}
os.close();
os = null;
fconn.close();
fconn = null;
return "OK";
}
catch (IOException ioe)
{
return("NOK " + ioe.getMessage());
}
}
private int checkCard()
{
int returnvalue=0;
String root = null;
Enumeration e = FileSystemRegistry.listRoots();
while (e.hasMoreElements())
{
root = (String) e.nextElement();
if( root.equalsIgnoreCase("sdcard/") )
{
returnvalue = returnvalue + 2;
}
else if( root.equalsIgnoreCase("store/") )
{
returnvalue = returnvalue + 1;
}
}
return returnvalue;
}
}
08-30-2010 06:38 AM
Thanks for All !!!
The data entered in the Edit field is succsessfully written in to the Text File.
Regards,
Pavan Kumar
08-30-2010 07:09 AM
dont just say thanks. press the kudos button for those who deserves it![]()