【问题标题】:Why is this BufferedReader returning empty String instead of null?为什么这个 BufferedReader 返回空字符串而不是 null?
【发布时间】:2013-01-19 15:11:10
【问题描述】:

我有一个 BufferedReader 遍历 CSV 文件的行;当它到达文件末尾时,它返回以下错误:

Exception in thread "main" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)

如何让读者意识到它已到达文件末尾并且输入为空而不是空?我检查了文件,最后一行末尾没有空格。

代码:

    File filReadMe = new File(inputFile);
    BufferedReader brReadMe = new BufferedReader(new InputStreamReader(new FileInputStream(filReadMe), "UTF-8"));

    try
    {
        String strLine;

        while ((strLine = brReadMe.readLine()) != null)
        {
            System.out.println(strLine);
            //place the line into CsvRecordFactory
            int record = csv.processLine(strLine, input); 
        }
    }
    catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        //Close the BufferedReader
        try {
            if (brReadMe != null)
                brReadMe.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

【问题讨论】:

  • 文件的最后一行包含一个空的String,因此您应该检查strLine.equals("") 是否作为停止while-loop 的另一个条件。
  • strLine.isEmpty() 在语义上会比 strLine.equals("")
  • 感谢 Luiggi 和 Cyrille。你知道我的语法有什么问题吗?我仍然看到同样的错误。 while ((strLine = brReadMe.readLine()) != null && !strLine.isEmpty())
  • @Luiggi Mendoza - 如果 strLine 为 null,则通常最好使用 "".equals(strLine) 来避免潜在的 NPE。
  • 尝试将System.out.println(strLine); 更改为System.out.println("\"" + strLine + "\""); 以帮助确认从文件中检索到的每一行的值。

标签: java null bufferedreader string


【解决方案1】:

如果文件的末尾是换行符,请尝试将其退格到上一行的末尾,看看是否仍然出现错误

【讨论】:

  • 这更像是一个 hack,而不是一个解决方案 =\.
【解决方案2】:

您可以通过这种方式简单地修改您的代码:

while ((strLine = brReadMe.readLine()) != null)
{
    if (!strLine.isEmpty()) {
        System.out.println(strLine);
        //place the line into CsvRecordFactory
        int record = csv.processLine(strLine, input); 
    }
}

这样,您的代码将忽略所有空行,而不仅仅是文件末尾的那些。

【讨论】:

  • 感谢西里尔。完全有道理。我的代码一定是在做一些奇怪的事情,因为即使在进行了建议的更改后,我仍然看到同样的错误。
  • 尝试在SSCCE 中重现问题,以便我们查看。
  • 感谢大家的帮助。我正在编辑代码以按要求创建 SSCCE,发现当我删除了 int record = csv.processLine(strLine, input);行,我不再有错误。我会看看它,看看它在做什么。感谢大家提供非常快速且非常有用的提示。
【解决方案3】:

尝试给它一个小的if条件来检查字符串是否为空。

版本1:检查字符串是否为空

  while ((strLine = brReadMe.readLine()) != null)
    {
          if(!strLine.isEmpty()){
              System.out.println(strLine);
              //place the line into CsvRecordFactory
              int record = csv.processLine(strLine, input); 
          }
    }

我也有代码版本 2,它检查字符串是否为数字,否则如果它们是字符或空或其他任何内容,if 中的代码将被忽略

while ((strLine = brReadMe.readLine()) != null)
        {
              if(strLine.matches("\\d+")){
                  System.out.println(strLine);
                  //place the line into CsvRecordFactory
                  int record = csv.processLine(strLine, input); 
              }
        }

【讨论】:

  • 抱歉延迟回复。当我输入正则表达式时,eclipse 大发雷霆,我花了一段时间才弄明白。选项 1 对我返回相同的错误,不幸的是我无法测试选项 2。
【解决方案4】:

问题不在于文件结尾。问题是您正在处理一个空白行,就好像它不是空白一样。可以想象,这可能发生在任何地方,而不仅仅是作为 EOF 之前的最后一行。在开始解析之前检查该行是否为空。

【讨论】:

    猜你喜欢
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    相关资源
    最近更新 更多