Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read strings in J2ME?

I'm using the MIDP 2.0 (JSR 118) and I just noticed that there is no reader for strings in J2ME.

Does anyone know how you are supposed to read Strings from an InputStream or InputStreamReader in a platform independent way (i.e. between two java enabled cell phones of different models)?

like image 504
Spoike Avatar asked Dec 27 '25 14:12

Spoike


2 Answers

Which profile are you using? The MID profile in JSR 118 specifies InputStreamReader (not StringReader, but that wouldn't help you read from an InputStream anyway).

EDIT: To reflect the change to the question :)

You use InputStreamReader.read(char[], int, int) and when you've read all you want to, create a new string from a char array. If you want to read a line at a time as you would from BufferedReader, you basically need to implement the functionality of BufferedReader yourself (keeping a buffer of "read but not consumed" chars) and keep reading until you hit a line break.

like image 86
Jon Skeet Avatar answered Dec 31 '25 03:12

Jon Skeet


Alternatively have a look at DataInputStream.readUTF().

It does required that the string being read off the InputStream be encoded appropriately (as in by a corresponding DataOutputStream.writeUTF(String)) so it might not be what you're looking for - but it does work across different phones/models etc.

like image 36
tonys Avatar answered Dec 31 '25 03:12

tonys



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!