Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
rpavankumar
Posts: 22
Registered: ‎08-10-2010
My Device: Not Specified
Accepted Solution

Writing to a text file in SDCard

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.txt";

 

 

 

try
{
FileConnection fconn=(FileConnection)Connector.open(filepath,Connector.READ_WRITE);
if(!fconn.exists())
{
fconn.create();
}
OutputStream os=fconn.openOutputStream();
os.write(editfield.getText().getBytes());
os.close();
fconn.close();

 

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.

Please use plain text.
Contributor
oshuma
Posts: 11
Registered: ‎07-22-2010
My Device: Curve
My Carrier: Verizon

Re: Writing to a text file in SDCard

Did you setup the simulator to emulate an SD card?

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Writing to a text file in SDCard

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.

Please use plain text.
Contributor
rpavankumar
Posts: 22
Registered: ‎08-10-2010
My Device: Not Specified

Re: Writing to a text file in SDCard

Yes.

In the Memory of simulator i had specified the path.

 

Regards,

Pavan Kumar

Please use plain text.
Contributor
rpavankumar
Posts: 22
Registered: ‎08-10-2010
My Device: Not Specified

Re: Writing to a text file in SDCard

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,Connector.READ_WRITE);

if(!fconn.exists())

{

fconn.create();

}

OutputStream os=fconn.openOutputStream();

os.write(editfield.getText().getBytes());

os.write("testcontentWelcome".getBytes());

//os.close();

 

}

 

catch(IOException ioe)

{

 

}

 

}

 

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Writing to a text file in SDCard

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.

Please use plain text.
Contributor
rpavankumar
Posts: 22
Registered: ‎08-10-2010
My Device: Not Specified

Re: Writing to a text file in SDCard

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

 

Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Writing to a text file in SDCard

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; 
  }
}

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Contributor
rpavankumar
Posts: 22
Registered: ‎08-10-2010
My Device: Not Specified

Re: Writing to a text file in SDCard

Thanks for All !!!

 

The data entered in the Edit field is succsessfully written in to the Text File.

 

Regards,

Pavan Kumar

Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Writing to a text file in SDCard

dont just say thanks. press the kudos button for those who deserves it:smileyhappy:

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.