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

我正在使用 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.


哔哔one
浏览 521回答 5
5回答

人到中年有点甜

我认为你的 . 有问题removeWhiteSpaces(i + 1, text);,如果你在字符串过程中有问题,读者将无法阅读下一行。你不检查空字符串,你打电话text.charAt(text.length()-1),这也是一个问题。打印异常,更改您的 catch 块以写出异常:} catch (Exception e) {&nbsp; &nbsp; e.printStackTrace();}原因是在你的while(text.charAt(j) == ' ') {,你不检查StringBuilder的长度,但你删除它......

慕虎7371278

问题不在于您的代码,而在于对readLine()方法的理解。在文档中指出:读取一行文本。一行被认为是由换行符 ('\n')、回车符 ('\r') 或紧跟换行符的回车符中的任何一个终止的。https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()这意味着如果该方法找到一个空行,它将停止读取并返回null。@tijn167 提出的代码将使用BufferedReader.&nbsp;如果您不克制BufferedReader使用ScanReader@Abhishek Soni 的建议。此外,您的方法removeWhiteSpaces()是检查空格,而空行不是空格,而是进位返回\r或换行\n或两者兼而有之。所以你的条件text.charAt(j) == ' '永远不会满足。

慕码人8056858

归一化功能导致了这种情况。对其进行以下调整应该可以解决此问题:public static String normalize(String string) {&nbsp; &nbsp; &nbsp; &nbsp; if(string.length() < 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder text = new StringBuilder(string.trim());&nbsp; &nbsp; &nbsp; &nbsp; if(text.length() < 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < text.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(text.charAt(i) == ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeWhiteSpaces(i + 1, text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(text.charAt(text.length() - 1) != '.') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text.append('.');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; text.append("\n");&nbsp; &nbsp; &nbsp; &nbsp; return text.toString();&nbsp; &nbsp; }

尚方宝剑之说

尝试这个:&nbsp; &nbsp;while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; if(line.trim().isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string += normalize(line);&nbsp; &nbsp; &nbsp; }

幕布斯7119047

试试 ScanReader&nbsp;Scanner scan = new Scanner(is);&nbsp;int rowCount = 0;&nbsp;while (scan.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String temp = scan.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(temp.trim().length()==0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}}//其余的逻辑
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java