Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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
java-BufferedReader readLine遇到空字符串时停止读取_Java_File_Bufferedreader_Readline - Fatal编程技术网

java-BufferedReader readLine遇到空字符串时停止读取

java-BufferedReader readLine遇到空字符串时停止读取,java,file,bufferedreader,readline,Java,File,Bufferedreader,Readline,我使用BufferedReader逐行读取文本文件。然后我使用一种方法来规范化每行文本。但我的规范化方法有问题,在调用它之后,BufferedReader对象停止读取文件。有人能帮我吗 这是我的密码: public static void main(String[] args) { String string = ""; try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {

我使用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);
        }
    }
试试这个:

   while ((line = br.readLine()) != null) {
        if(line.trim().isEmpty()) {
            continue;
        }
         string += normalize(line);
      }
试一试扫描阅读器

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

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

//文件第二行的其余逻辑是空的,因此while循环停止

我认为您的removeWhiteSpacesi+1,text;,中存在问题;,如果您在字符串处理过程中遇到问题,读者将无法阅读下一行

如果不检查空字符串,则调用text.charAttext.length-1,这也是一个问题

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

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

原因是您的whiletext.charAtj=''{,您不检查StringBuilder的长度,但删除了它…

规范化函数导致了这种情况。 下面的调整应该可以解决这个问题:

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”或紧接着换行符的回车符中的任意一个终止

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

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


此外,您的方法removeWhiteSpaces正在检查空白,而空行不是空白,而是进位返回\r或换行\n或两者兼而有之。因此您的条件text.charAtj==''永远不会满足。

没有异常,只是挂起!?removeWhiteSpaces到底做什么?@xerx593没有异常,甚至挂起。它正常运行,但只有p打印出第一行:abc。不要只捕获异常而不做任何处理。至少要执行e.printStackTrace,否则它只会默默地失败,你永远不会知道原因。你在removeWhiteSpaces中得到一个IndexOutOfBounds,你在主方法中默默地捕获它。字符串的长度可以大于1,然后trim被称为nd将为零。添加另一个if作为修复如果该行为空,它将返回一个空字符串,但会继续,直到文件完成。它不会返回null,这会导致循环终止。真正的问题是隐藏任何问题的吞没异常。
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();
    }