为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

哔哔one 2022-06-15 16:00:51
我正在使用 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.
查看完整描述

5 回答

?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

我认为你的 . 有问题removeWhiteSpaces(i + 1, text);,如果你在字符串过程中有问题,读者将无法阅读下一行。


你不检查空字符串,你打电话text.charAt(text.length()-1),这也是一个问题。


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


} catch (Exception e) {

    e.printStackTrace();

}

原因是在你的while(text.charAt(j) == ' ') {,你不检查StringBuilder的长度,但你删除它......


查看完整回答
反对 回复 2022-06-15
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

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

读取一行文本。一行被认为是由换行符 ('\n')、回车符 ('\r') 或紧跟换行符的回车符中的任何一个终止的。

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

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

@tijn167 提出的代码将使用BufferedReader. 如果您不克制BufferedReader使用ScanReader@Abhishek Soni 的建议。

此外,您的方法removeWhiteSpaces()是检查空格,而空行不是空格,而是进位返回\r或换行\n或两者兼而有之。所以你的条件text.charAt(j) == ' '永远不会满足。


查看完整回答
反对 回复 2022-06-15
?
慕码人8056858

TA贡献1803条经验 获得超6个赞

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


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();

    }


查看完整回答
反对 回复 2022-06-15
?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

尝试这个:


   while ((line = br.readLine()) != null) {

        if(line.trim().isEmpty()) {

            continue;

        }

         string += normalize(line);

      }


查看完整回答
反对 回复 2022-06-15
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

试试 ScanReader


 Scanner scan = new Scanner(is);

 int rowCount = 0;

 while (scan.hasNextLine()) {

             String temp = scan.nextLine();


             if(temp.trim().length()==0){

                 continue;

             }

}

//其余的逻辑


查看完整回答
反对 回复 2022-06-15
  • 5 回答
  • 0 关注
  • 520 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号