java 逐行读取文件-到达最后一行后终止while循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28705491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading file line by line- terminating while loop after reaching last line
提问by Hyman
Program to read file and print all the characters that are letters, throws a NullPointerException when it gets to the last line.
读取文件并打印所有字母字符的程序,在到达最后一行时抛出 NullPointerException。
import java.io.*;
public class Foo {
public static void main(String[] args) throws IOException {
FileReader file = new FileReader(new File("source.txt"));
BufferedReader read = new BufferedReader(file);
String line = read.readLine();
while (line != null) {
for (int i = 0; i < line.length(); i++) {
line = read.readLine(); // this is where the problem is. When it reaches the last line, line = null and the while loop should terminate!
if (Character.isLetter(line.charAt(i))) {
System.out.print(line.charAt(i));
}
}
}
}
}
回答by Piotr Zych
Just realize you can do like this:
只要意识到你可以这样做:
String line = null;
while ((line = read.readLine()) != null) {
for(int i=0; i<line.length(); ++i)
{
if(Character.isLetter(line.charAt(i)))
System.out.println(line.charAt(i));
}
}
Do not forget to close streams, and it would be better to encapsulate everything in try block.
不要忘记关闭流,最好将所有内容都封装在 try 块中。
回答by chancea
While loops do not work like how you explained them in your comment:
虽然循环不像您在评论中解释它们的方式工作:
// this is where the problem is. When it reaches the last line, line = null and the while loop should terminate!
// 这就是问题所在。当它到达最后一行时, line = null 并且 while 循环应该终止!
While loops only check the condition(s) at the startof each iteration. They will not terminate mid loop just because the condition will be false at the start of the next iteration.
While 循环只在每次迭代开始时检查条件。它们不会仅仅因为条件在下一次迭代开始时为假而终止中循环。
So this null check you have at the start while (line != null)
will only and always happen at the startof each iteration, even if line
was set to null
mid iteration
所以你在开始while (line != null)
时的这个空检查只会并且总是发生在每次迭代的开始,即使line
被设置为null
中间迭代
So as others have shown you can either construct your while loop as:
因此,正如其他人所展示的,您可以将 while 循环构建为:
String line = null;
while ((line = read.readLine()) != null)
{
for (int i = 0; i < line.length(); i++)
{
if (Character.isLetter(line.charAt(i)))
{
System.out.print(line.charAt(i));
}
}
}
and remove all other read.readLine()
statements from your code. (which is the shortest lines of code).
并read.readLine()
从您的代码中删除所有其他语句。(这是最短的代码行)。
Or if you want to be more explicit for perhaps more readability, you can keep the initial read.readLine()
as you have it but move the iterative read.readLine()
to after all your uses for line are done:
或者,如果您想更明确以提高可读性,您可以保持初始状态read.readLine()
,但在read.readLine()
完成所有对 line 的使用后将迭代移动到:
String line = read.readLine();
while (line != null)
{
for (int i = 0; i < line.length(); i++)
{
if (Character.isLetter(line.charAt(i)))
{
System.out.print(line.charAt(i));
}
}
line = read.readLine();
//line is never used after this so an NPE is not possible
}
回答by T.Gounelle
Your for (int i = 0; i < line.length(); i++)
does not make sens here. The length of a line has nothing to do with the number of lines in the file.
你for (int i = 0; i < line.length(); i++)
在这里没有意义。一行的长度与文件中的行数无关。
Change your code to :
将您的代码更改为:
String line = null;
while ((line = readLine()) != null) {
System.out.println(line.length());
// do what ever you need with line
}
回答by James
Try while((line = read.readLine()) != null)
尝试 while((line = read.readLine()) != null)
This initializes the value as its checked against the while condition at every loop iteration.
这会在每次循环迭代时根据 while 条件对值进行初始化。