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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Contributor
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.

Contributor
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?

Developer
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.

Contributor
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

Contributor
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)

{

 

}

 

}

 

Developer
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.

Contributor
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

 

Highlighted
Developer
Posts: 554
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-
Contributor
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

Developer
Posts: 554
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 itSmiley Happy

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-