Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 与J2me读取文件混淆。请帮我理解_File_Text_Java Me - Fatal编程技术网

File 与J2me读取文件混淆。请帮我理解

File 与J2me读取文件混淆。请帮我理解,file,text,java-me,File,Text,Java Me,我正在为Symbian S60手机开发一个J2ME应用程序,需要从文本文件中读取数据。我无法访问BufferedReader从文件中提取一行文本,但我确实在诺基亚帮助论坛中发现了这一点,这让我有点困惑。这是代码,我的问题在下面。谢谢你的回答 /** * Reads a single line using the specified reader. * @throws java.io.IOException if an exception occurs when re

我正在为Symbian S60手机开发一个J2ME应用程序,需要从文本文件中读取数据。我无法访问BufferedReader从文件中提取一行文本,但我确实在诺基亚帮助论坛中发现了这一点,这让我有点困惑。这是代码,我的问题在下面。谢谢你的回答


    /**
     * 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();
    }
    
我的问题是关于readLine()方法的。在它的while()循环中,为什么我必须检查readChar!=-1和!='\不?我的理解是-1代表流的结束(EOF)。我的理解是,如果我提取一行,我只需要检查换行符


谢谢。

请仔细阅读代码文档。你所有的疑问都在这方面得到了很好的回答


函数正在检查'-1',因为它正在处理没有新行字符的流。在这种情况下,它将以字符串形式返回整个流

请仔细阅读代码文档。你所有的疑问都在这方面得到了很好的回答

函数正在检查'-1',因为它正在处理没有新行字符的流。在这种情况下,它将以字符串形式返回整个流

这正是你(喜欢)将逻辑应用于你试图做/实现的事情的方式。例如,上面的例子可以写得像他的一样


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();
        }
    }
所以前面的代码示例readChar-1检查只是安全检查。

这正是您(喜欢)将逻辑应用于您试图做/实现的事情的方式。例如,上面的例子可以写得像他的一样


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();
        }
    }
所以前面的代码示例readChar检查-1只是安全检查