Java BufferedReader 到字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25370256/
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
Java BufferedReader to String Array
提问by Cappuccino90
I was looking through a lot of diffrent subjects here on stackoverflow but couldn't find anything helpful so far :/
我在stackoverflow上浏览了很多不同的主题,但到目前为止找不到任何有用的东西:/
So this is my problem. I am writing a filecopier. The problem occurs already at reading the file. My test docoument got 3 lines of random text. All those 3 lines should get written in a string array. The problem is that only the 2nd line of the textdocument gets written in the array and I can't figure out why. Already debugged it, but didn't get me any further.
所以这是我的问题。我正在写一个文件复制器。读取文件时已经出现问题。我的测试文档有 3 行随机文本。所有这 3 行都应该写在一个字符串数组中。问题是只有 textdocument 的第二行被写入数组,我不知道为什么。已经调试了它,但没有让我进一步。
I know there are diffrent solutions for a filecopier with diffrent classes etc. But I would really like to get it running with the classes I used here.
我知道对于具有不同类等的文件复制器有不同的解决方案。但我真的很想让它与我在这里使用的类一起运行。
String[] array = new String[5];
String datei = "test.txt";
public String[] readfile() throws FileNotFoundException {
FileReader fr = new FileReader(datei);
BufferedReader bf = new BufferedReader(fr);
try {
int i=0;
//String Zeile = bf.readLine();
while(bf.readLine() != null){
array[i] = bf.readLine();
// System.out.println(array[i]); This line is for testing
i++;
}
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
return array;
采纳答案by Matt Ball
You're calling readLine()
twice for each iteration of the loop, thereby discarding every other line. You need to capture the value returned by everycall to readLine()
, because each readLine()
call advances the reader's position in the file.
您readLine()
为循环的每次迭代调用两次,从而丢弃每隔一行。您需要捕获每次调用返回的值readLine()
,因为每次readLine()
调用都会提高读取器在文件中的位置。
Here's the idiomaticsolution:
这是惯用的解决方案:
String line;
while((line = bf.readLine()) != null){
array[i] = line;
i++;
}
回答by Jens
Here you read 2 lines:
在这里,您阅读了 2 行:
while(bf.readLine() != null){
array[i] = bf.readLine();
// System.out.println(array[i]); This line is for testing
i++;
}
You have to change your Code to:
您必须将代码更改为:
String line = null;
while((line =bf.readLine()) != null){
array[i] = line;
// System.out.println(array[i]); This line is for testing
i++;
}
回答by Himanshu Tyagi
The problem is here :
问题在这里:
while(bf.readLine() != null)
readLine()
reads a line and returns the same at the same time it moves to the next line.
readLine()
读取一行并在移动到下一行的同时返回相同的内容。
So instead of just checking if the returned value was null
also store it.
因此,不仅仅是检查返回的值是否null
也存储了它。
String txt = null;
while((txt = bf.readLine()) != null)
array[i++] = txt;
回答by user1219387
I think its because you are calling readLine() twice. First time in the loop, and then second time when you put it in the array. So, it reads a line at the beginning of the loop (line 1), then first line of code inside the loop (line 2 that you see)
我认为这是因为您两次调用 readLine() 。第一次在循环中,然后第二次将它放入数组中。因此,它在循环开始时读取一行(第 1 行),然后是循环内的第一行代码(您看到的第 2 行)