开发者

Confusion with J2me reading file. Please Help me understand

开发者 https://www.devze.com 2023-03-01 15:52 出处:网络
I am working on a J2ME app for Symbian S60 phones where reading from a text file is required.I have no access to BufferedReader to extract a line of text from a file, but I did find this in the Nokia

I am working on a J2ME app for Symbian S60 phones where reading from a text file is required. I have no access to BufferedReader to extract a line of text from a file, but I did find this in the Nokia help forums, and it has me a bit confused. Here's the code, and my question is below. Thanks for answering.


    /**
     * Reads a single line using the specified reader.
     * @throws java.io.IOException if an exception occurs when reading the
     * line
     */
    private String readLine(InputStreamReader reader) throws IOException {
        // Test whether the end of file has been reached. If so, return null.
        int readChar = reader.read();
        if (readChar == -1) {
            return null;
        }
        StringBuffer string = new StringBuffer("");
        // Read until end of file or new line
        while (readChar != -1  && readChar != '\n') {
            // Append the read character to the string. Some operating systems
            // such as Microsoft Windows prepend newline character ('\n') with
            // carriage return ('\r'). This is part of the newline character
            // and therefore an exception that should not be appended to the
            // string.
            string.append((char)readChar);

            // Read the next character
            readChar = reader.read();
        }
        return string.toString();
    }
    

My question is regarding the readLine() method. In its while() loop, why 开发者_如何学Pythonmust I check that readChar != -1 and != '\n' ? It is my understanding that -1 represents the end of the stream (EOF). I was my understanding that if I am extracting one line, I should only have to check for the newline char.

Thanks.


Please read the code documentation carefully. All your doubts are well answered in that.

The function is checking the ‘-1’ because it is taking care of those streams which comes without a new line character. In that case it will return the whole stream as a string.


It is just way how you (like to) apply logic to what you try to do/achieve. For example above example could have been written like his


private String readLine(InputStreamReader reader) throws IOException {
        // Test whether the end of file has been reached. If so, return null.
        int readChar = reader.read();
        if (readChar == -1) {
            return null;
        }else{
            StringBuffer string = new StringBuffer("");

            // Read until end of file or new line
            while (readChar != '\n') {
                // Append the read character to the string. Some operating systems
                // such as Microsoft Windows prepend newline character ('\n') with
                // carriage return ('\r'). This is part of the newline character
                // and therefore an exception that should not be appended to the
                // string.
                string.append((char)readChar);

                // Read the next character
                readChar = reader.read();
            }
            return string.toString();
        }
    }

So previous code sample readChar check for -1 is just safeguard check.

0

精彩评论

暂无评论...
验证码 换一张
取 消