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
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none
Accepted Solution

How to read an int from a file

Hello programers,

 

I am facing a probably very simple problem but which has kept me stuck for some time now, hence I am now seeking your help.

int i=0;

try {
             FileConnection fconn =  (FileConnection)Connector.open(System.getProperty("fileconn.dir.photos")+"increment.txt", Connector.READ_WRITE);
             if (fconn.exists()){
                 OutputStream os=fconn.openOutputStream();
                 String value="1";
                 os.write(value.getBytes());
                 os.close();
                 InputStream is=fconn.openInputStream();
                 i=Integer.parseInt(is.toString());
                 is.close();
             }
             if (!fconn.exists()){
                 fconn.create();}
             fconn.close();
         }
         catch (IOException ioe) {}

I know the photo file is not the best idea, it is just a test. So, when i go into the increment.txt file, I do see that a "1" is writen, however, when I try to run it I get a NumberFormatException, do you know why, and how I can solve this issue ?

 

Thanks a lot for any assistance, I will not forget the kudos... Smiley Wink

Developer
Posts: 16,985
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to read an int from a file

take a look at the string in the debugger, maybe there is a line break or something included?

you could try to call trim maybe before parseInt

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none

Re: How to read an int from a file

Okay now I'm truly lost, my string seems to be: net.rim.device.internal.io.store.ContentStoreInputStream@86de549a

 

What would that mean?

 

Also, i tried the read() function on the input stream, it returned 61. But this might not help much.

 

Sorry for all the trouble!

Developer
Posts: 16,985
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to read an int from a file

the code you posted writes the file. maybe you should post the code you use to read it Smiley Happy

(an input stream is not a string)

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none

Re: How to read an int from a file

Oh, so you mean my inputStream.tostring() will not give me the actual content of the .txt file?

 

Then should I use read(byte[] b), to read the content?

 

Sorry for all those very silly questions, i'm really new to programing in general... Smiley Sad

Developer
Posts: 16,985
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to read an int from a file

yes, this is correct. you have to read from the inputstream.

google for inputstream string or something like that, there are plenty of examples that can help you out.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none

Re: How to read an int from a file

Thanks for all the advice Simon, it really helps.

 

So, i have found this code on several websites, i'm guessing it is the proper way to go:

 

public static String convertStreamToString(InputStream isthrows Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line + "\n");
    }
    is.close();
    return sb.toString();
  }

The thing is that on the SDK there is no such thing as BufferedReader or StringBuilder, so I have tried putting BufferedInputStream instead of BufferedReader and StringBuffer instead of StringBuilder, is this a good idea? Furthermore, the SDK tells me it does not recognize BufferedInputStream, which is wierd because it is in the API documentation.


Any clue on how to translate that code to make it appropriate?

 

Thanks a thousand time again for your concern!

Developer
Posts: 16,985
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to read an int from a file

the example is for desktop java, you have to find one for j2me.

 

i think you can use this class to read the inputstream:

http://www.blackberry.com/developers/docs/5.0.0api/java/io/InputStreamReader.html

 

there is no buffered reader in j2me/bb, you can just read the string char by char and add them all to a stringbuffer.

when you are finished call stringbuffer.toString

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Highlighted
Contributor
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none

Re: How to read an int from a file

Yes indeed you are right, it works!

 

Thanks a lot once again Simon, you prove to be everhelpfull in my daily programing endeavours.

 

Here is the final code that worked for me:

 

InputStream is=fconn.openInputStream();
                 InputStreamReader isr = new InputStreamReader(is,"UTF8");
                 StringBuffer buffer = new StringBuffer();
                 int ch;
                 while ((ch = isr.read()) > -1) {
                     buffer.append((char)ch);}

                 temp=buffer.toString();
                 temp.trim();
                 i=Integer.parseInt(temp);
                 is.close();

Kudos to you, of course... Smiley Wink