- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有这个简单的方法:
private String read(String filePath) throws IOException {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String fileContent = "";
StringBuilder stringBuilder = new StringBuilder(fileContent);
while (bufferedReader.readLine() != null){
System.out.println(bufferedReader.readLine());
stringBuilder.append(bufferedReader.readLine());
}
return fileContent;
}
正如您在第 8 行中看到的,我包含了 print 用于调试目的我希望此方法从此 txt 文件返回字符串:
1a1
2b 2a
3c 3b 3a
4d 4cr 4bb4 4a
5e 5d 5c 5b 5ax
6f 6ea 6d 6ca 6bb 6a
7g 7f 7ea
出于某种原因输出是这样的:
2b 2a
5e 5d 5c 5b 5ax
null
为什么它只读取第二行和第五行?这个 null 来自哪里?最后返回的字符串似乎是空的。
我想了解这里发生了什么。谢谢 :)
最佳答案
这是因为您在 while
循环的 null
检查中每隔三行使用一次,打印从二开始的每三行,并附加从三开始的每三行,例如这个:
1 null check
2 print
3 append
4 null check
5 print
6 append
7 null check
8 print
9 append
... and so on
您应该将读取的行保存在 String
变量中,并在循环中使用它,如下所示:
String line;
while ((line = bufferedReader.readLine()) != null){
System.out.println(line);
stringBuilder.append(line);
}
现在循环头结合了一个赋值和一个null
检查,从readLine
赋值的变量表示从文件中读取的最后一行。
关于java - BufferReader 意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53791844/
我有一个可以将某些字符加粗的函数。一切工作正常,除了我在方法开始时的字符串被剪切到某处并返回一半,而不是作为开始时得到的整个字符串。这是代码,我不知道我的错误在哪里。 public String bo
我正在从输入(IDE eclipse Luna)输入行,按下回车键,光标一直向下移动,但没有输出显示。在尝试的第二种方法中,但我必须按两次 enter 才能打印输出,我该如何修复这两个错误。为什么在第
我有一个有点简单的服务器,这意味着我正在尝试通过使服务器尽可能面向对象来学习不同的设计模式。令人惊讶的是,在我创建方法 close() 之前我还没有遇到过一个问题。 很明显,当客户端关闭与数据库的连接
我正在尝试使用 BufferReader 填充二维数组,它成功填充了数组的所有行,除了最后一行之外。我尝试将循环增加一以解决该行,但出现索引越界错误。我怎样才能显示最后一行? public stati
我有这个简单的方法: private String read(String filePath) throws IOException { FileReader fileReader = new
我有一个问题正在解决。我必须使用 bufferedReader 将单词添加到树集中(并输出树集的大小),但问题是我无法通过编译器速度测试限制。文本仅包含字母和空格(可以是空行)。我必须找到一个新的解决
我使用 while 循环逐行搜索文件中的内容,直到找到字符串“MATCH”。但是当在另一个函数中我需要再次使用 BufferReader 时,它会引用文件末尾还是从文件开头开始? Public Str
我正在尝试处理我用 BufferReader 读取的 html,如下所示: try { HttpResponse response = DisplayMessageAc
是否可以为缓冲区读取器设置多个标记,以便我可以多次预读? 所以我的代码比较长,但基本思路是在设置一个我将要返回的标记之后,我提前阅读。我的代码检查此“前面”行是否满足某些条件,如果满足,它会在返回初始
全部,我正在尝试读取一个将由多个线程写入的文件,我将使用 BufferedReader 在一个线程中读取该文件。 代码如下所示。 FileReader reader = new FileReader(
java BufferReader 有一个方法 readLine 读取到 '\n' 或 '\r' 是读取然后返回行字符串。示例: InputStreamReader isr = new InputSt
使用以下代码将读取器的输出拆分为字符串,然后在构造函数中使用这些字符串。但是,当 bufferreader 到达流的末尾时,它会输出 null,这会导致构造函数中出现空指针异常。 List resul
我可以看到以下两个程序之间的性能差异巨大。 import java.io.*; import java.util.Date; class SkipProg2 { public static vo
我正在使用 java 中的 HttpURLConnection 读取一个大型 xml 文件,如下所示。 StringBuilder responseBuilder = new StringBuilde
I wrote some program that has to receive and send messages. But I have some troubles with streams
我正在尝试从文本文件中提取特定数据。我使用 bufferreader 逐行读取文件,以便我可以处理该行。 以下是我编写的代码: File CAD = new File (filename);
我正在制作一个 HTTP 服务器和 HTTP Web 客户端,用于简单的 Http 请求和响应。 这是服务器的代码 import java.io.*; import java.net.*; impor
如何使用 FileReader 而不是使用 BufferReader 我想将 File,FileReader 用于此从 ftp 读取文件的程序 public class FileReader
我是java新手,我尝试用TCP Socket编写一个简单的客户端服务器,但经过几天的努力后我遇到了麻烦,我无法解决这个问题。 我使用 BufferReader.Readline(); 得到 null
我正在制作一个游戏,直到用户在命令行中输入退出为止。 用户可以输入不同的命令,例如 get 和 go,使用 get 命令,用户可以说出想要什么,获取棒球棒。我在代码中所做的就是拆分命令。 一切工作正常
我是一名优秀的程序员,十分优秀!