【问题标题】:Confusion with J2me reading file. Please Help me understand与 J2me 读取文件混淆。请帮我理解
【发布时间】:2011-05-01 04:49:17
【问题描述】:

我正在为 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 和 != '\n' ?据我了解,-1 代表流的结束(EOF)。我的理解是,如果我要提取一行,我应该只需要检查换行符。

谢谢。

【问题讨论】:

    标签: file text java-me


    【解决方案1】:

    请仔细阅读代码文档。你所有的疑惑都在这里得到了很好的解答。

    该函数正在检查“-1”,因为它正在处理那些没有换行符的流。在这种情况下,它将整个流作为字符串返回。

    【讨论】:

      【解决方案2】:

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

      
      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 只是安全检查。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 2017-07-02
        • 1970-01-01
        • 2012-09-14
        • 1970-01-01
        相关资源
        最近更新 更多