繁体   English   中英

java-BufferedReader readLine在encouters空字符串时停止读取

[英]java - BufferedReader readLine stop reading when encouters empty string

我正在使用BufferedReader逐行读取文本文件。 然后,我使用一种方法来规范化每行文本。 但是我的规范化方法有问题,在调用它之后,BufferedReader对象停止读取文件。 有人可以帮我弄这个吗。

这是我的代码:

public static void main(String[] args) {
    String string = "";

    try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
        String line;
        while ((line = br.readLine()) != null) {

            string += normalize(line);

        }
    } catch (Exception e) {

    }
    System.out.println(string);
}

public static String normalize(String string) {

    StringBuilder text = new StringBuilder(string.trim());



    for(int i = 0; i < text.length(); i++) {
        if(text.charAt(i) == ' ') {
            removeWhiteSpaces(i + 1, text);
        }
    }

    if(text.charAt(text.length() - 1) != '.') {
        text.append('.');
    }

    text.append("\n");
    return text.toString();
}

public static void removeWhiteSpaces(int index, StringBuilder text) {
        int j = index;
        while(text.charAt(j) == ' ') {
            text.deleteCharAt(j);
        }
    }

这是我使用的文本文件:

abc .

 asd.



 dasd.

我认为您的removeWhiteSpaces(i + 1, text);有问题removeWhiteSpaces(i + 1, text); ,并且如果您在字符串处理过程中遇到问题,阅读器将无法阅读下一行。

您不检查空字符串,而是调用text.charAt(text.length()-1) ,这也是一个问题。

打印异常,更改catch块以写出异常:

} catch (Exception e) {
    e.printStackTrace();
}

原因是在您的while(text.charAt(j) == ' ') { ,您没有检查StringBuilder的长度,但是将其删除了...

尝试这个:

   while ((line = br.readLine()) != null) {
        if(line.trim().isEmpty()) {
            continue;
        }
         string += normalize(line);
      }

尝试使用ScanReader

 Scanner scan = new Scanner(is);
 int rowCount = 0;
 while (scan.hasNextLine()) {
             String temp = scan.nextLine();

             if(temp.trim().length()==0){
                 continue;
             }
}

//其余的逻辑

归一化功能导致了这种情况。 对其进行以下调整可以解决此问题:

public static String normalize(String string) {

        if(string.length() < 1) {
            return "";
        }
        StringBuilder text = new StringBuilder(string.trim());
        if(text.length() < 1){
            return "";
        }


        for(int i = 0; i < text.length(); i++) {
            if(text.charAt(i) == ' ') {
                removeWhiteSpaces(i + 1, text);
            }
        }

        if(text.charAt(text.length() - 1) != '.') {
            text.append('.');
        }

        text.append("\n");
        return text.toString();
    }

问题不在于您的代码,而在于对readLine()方法的理解。 在文档中说明:

读取一行文本。 一行被认为由换行符('\\ n'),回车符('\\ r')或回车符后紧跟换行符之一终止。

https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()

因此,这意味着如果该方法找到一个空行,它将停止读取并返回null

@ tijn167建议的代码将使用BufferedReader解决。 如果您不限制BufferedReader ScanReader按照@Abhishek Soni的建议使用ScanReader

同样,您的方法removeWhiteSpaces()将检查空白,而空行不是空白,而是进位返回\\r或换行\\n或两者。 因此,永远不会满足您的条件text.charAt(j) == ' '

文件的第二行为空,因此while循环停止

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM