How to read lines in J2ME?

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

Does anyone know how you should read lines from InputStreamor InputStreamReaderin a platform-independent way (i.e. between two cell phones with java support of different models)?

+3
source share
4 answers

Look alternatively DataInputStream.readUTF().

It is required that the line read from the InputStream be properly encoded (as in the corresponding one DataOutputStream.writeUTF(String)), so that it may not be what you are looking for - but it works with different phones / models, etc.

+3
source

? MID JSR 118 InputStreamReader ( StringReader, InputStream ).

EDIT: :)

InputStreamReader.read(char [], int, int), , , char. , BufferedReader, BufferedReader ( ", " ) , .

+5

... , .

, , . 5 , ...

, j2ME - , BufferedReader , , freakin ... , , 1 , .

( , , , )

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
    }
}

, is.read() , ONE BYTE AT TIME. , telnet , , char char StringBuffer , char 13.

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

, , j2ME. , - .

+3

?

InputStreamReader.read(char[], int, int), , , char. , BufferedReader, BufferedReader ( ", " ) , .

+1

Source: https://habr.com/ru/post/1698240/


All Articles