How do I read strings in J2ME?

拥有回忆 提交于 2019-12-18 09:03:06

问题


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


回答1:


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.




回答2:


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.




回答3:


Well... I know this was a LONG time ago.

You need to do exactly what John said, and it is VERY simple. It almost took me 5 hours to figure this one out the first time...

I still wonder why j2ME didn't include something as essential as the BufferedReader method for sockets, it's not like the freakin cellphones will crash with it... and yes, I don't give a rat's ass if my app runs 1ms slower than it should.

(I'm just going to put the relevant code, I assume you know how to form classes and import the required libraries)

ServerSocketConnection listener
    = (ServerSocketConnection)Connector.open("socket://:1235");
System.out.println("Waiting for connection...");
StreamConnection server = listener.acceptAndOpen();
InputStream is = server.openInputStream();

//Now comes the fake BufferedReader equivalent part

int ch = 0;
StringBuffer sb = new StringBuffer();

while ((ch = is.read()) != -1){
    sb.append((char)ch);
    if(sb.charAt(sb.length()-1) == 13 ) {
       //Carriage return was received or ENTER was pressed
       break; //Exit loop and print input
    }
}

As you can see, the is.read() method will lock the thread till new input is received from the user ONE BYTE AT A TIME. This means if you use telnet to test, each keystroke will make the loop iterate once, hence, we simply concatenate char by char in a StringBuffer until char 13 is received.

System.out.println(sb.toString());

I hope this helps people trying to do a socket server on j2ME. I already crafted a fully functional multithreaded version of this for blackberry, in case anyone needs it.




回答4:


Would you be able to provide an example of this?

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.



来源:https://stackoverflow.com/questions/200239/how-do-i-read-strings-in-j2me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!