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
Developer
Posts: 139
Registered: ‎05-25-2009
My Device: Not Specified
Accepted Solution

How to write data in a file in blackberry?

Dear Friends,

          I want to know how to write content in file. example:I am having one basic edit field and one save button if click the save button then it should write the content of basiceditfield into one text file and i can able to retrive that file in later.Can any one tell me how to do ? If u can provide me some code snippet.

 

regards,

s.kumaran.

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to write data in a file in blackberry?

Check the following code snippet to write a file...

FileConnection fc = (FileConnection) Connector.open(System.getProperty("file:///SDCard/"+"test.txt"),Connector.READ_WRITE); if(!fc.exists()) fc.create(); OutputStream os =fc.openOutputStream(); os.write(yourfield.getText()); os.close(); fc.close();

 

You can make use of read methods to read teh File content..

Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to write data in a file in blackberry?

Check this thread for reading...

 

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=2362&query.id=224...


Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 139
Registered: ‎05-25-2009
My Device: Not Specified

Re: How to write data in a file in blackberry?

thanks for ur immediate response and in ur reply "file:///SDCard/" this location refers to where? and  if it refers SDCard in blackberry means how to make it in simulator 9000.

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to write data in a file in blackberry?

Check this KB article.

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800738/800792/801083/...


Use Search. "Accept Solution" If the problem is resolved.
Highlighted
Developer
Posts: 139
Registered: ‎05-25-2009
My Device: Not Specified

Re: How to write data in a file in blackberry?

Hi i have read and tried the article but still i m having some problem.

 

In my system's " E: " i created one flolder with the name " SDCard "and i have specify this location ("E:\SDCard") at PCFilesystem path for SDCard files in the prefrence window  then i click ok.After that i can see one folder inside that SDCard folder with the name of blackberry and inside the blackberry folder i can see six folders with name of documents,music,picture,ringtones,videos,and voice chats.I created a file "test.txt" inside the Document folder and i specify the path as ("E:/SDCard/BlackBerry/Documents/"+"test.txt") in the FileConnection even i can get a error can u pls what i m doing wrong.

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to write data in a file in blackberry?

In your FileConnection, you have to mention file:///SDCard/... not e:/SDCard/...

Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 139
Registered: ‎05-25-2009
My Device: Not Specified

Re: How to write data in a file in blackberry?

i have tried all the following ya still i m getting null url exception ,

 

1) FileConnection fc=(FileConnection)Connector.open(System.getProperty("file:///SDCard/BlackBerry/documen/"+"test.txt"),Connector.READ_WRITE);

 

2)  FileConnection fc=(FileConnection)Connector.open(System.getProperty("file:E:/SDCard/BlackBerry/documents/"+"test.txt"),Connector.READ_WRITE);

 

3)FileConnection fc=(FileConnection)Connector.open(System.getProperty("file:/SDCard/BlackBerry/documents/"+"test.txt"),Connector.READ_WRITE);

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to write data in a file in blackberry?

Try this..

 

FileConnection fc = (FileConnection)Connector.open("file:///SDCard/BlackBerry/documents/"+"text.txt",Connector.READ_WRITE);


Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 139
Registered: ‎05-25-2009
My Device: Not Specified

Re: How to write data in a file in blackberry?

Hey thanks ya.Now file is creating ya but contents are not written in the file ya.

 

i try to write a stirng driectly in os.write(stringvalue) but it show the error then i refer the api then we can only able to write only byte and int so i convert my string in one byte array then i write into file using for loop but no content is writing ya.here is the part of the code which i have used.

 

 

 OutputStream os =fc.openOutputStream();
            byte[] barr=new byte[stringvalue.length()];
            for(int i=0;i<stringvalue.length();i++)
            {  
                barr[i]=(byte)s.charAt(i);
            }
            //os.write(yourfield.getText());
           for(int i=0;i<barr.length;i++)
           {
            os.write(barr[i]);
           }