2

I got a code from net and it uses

imports java.io.BufferedReader 
imports java.io.PrintWriter 

This code is in JAVA SE. But I need to do the same in J2ME(JAVA ME). But it doesn't have BufferedReader and PrintWriter?

What can be used instead of BufferedReader and PrintWriter?

gnat
  • 6,213
  • 108
  • 53
  • 73
Ivin
  • 4,435
  • 8
  • 46
  • 65
  • 2
    Maybe you'll want to look at this: http://stackoverflow.com/questions/200239/how-do-i-read-strings-in-j2me – Silviu Apr 23 '12 at 14:34

1 Answers1

2

If BufferedReader is there because of readLine method you can replace it by InputStreamReader and use a method like:


    public String readLine(Reader reader) throws IOException {
        StringBuffer line = new StringBuffer();
        int c = reader.read();

        while (c != -1 && c != '\n') {
            line.append((char)c);
            c = reader.read();
        }

        if (line.length() == 0 && c == -1) {
            return null;
        }

        return line.toString();
    }

If PrintWriter is there because of print methods you can replace it by PrintStream.

Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
  • I just came across this answer (yeah, I still have to work with J2ME). There is a bug in this snippet - it returns `null` on first empty line. Shouldn't the if condition be `line.length() == 0 && c == -1` ? – Code Painters Jun 01 '17 at 14:23
  • considering an empty line as one just containing a '\n', yes, it will return null on first empty line – Telmo Pimentel Mota Jun 06 '17 at 11:59