同时逐行读取两个文本文件-java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10831007/
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
Read two textfile line by line simultaneously -java
提问by alvas
I have 2 textfiles in two different languages and they are aligned line by line. I.e. the first line in the textfile1 should be equals to the first line in textfile2, and so on and so forth.
我有两种不同语言的 2 个文本文件,它们逐行对齐。即 textfile1 中的第一行应该等于 textfile2 中的第一行,依此类推。
Is there a way to read both file line-by-line simultaneously?
有没有办法同时逐行读取两个文件?
Below is a sample of how the files should look like, imagine the number of lines per file is around 1,000,000.
下面是文件外观的示例,假设每个文件的行数约为 1,000,000。
textfile1:
文本文件 1:
This is a the first line in English
This is a the 2nd line in English
This is a the third line in English
textfile2:
文本文件2:
C'est la première ligne en Fran?ais
C'est la deuxième ligne en Fran?ais
C'est la troisième ligne en Fran?ais
desired output
期望的输出
This is a the first line in English\tC'est la première ligne en Fran?ais
This is a the 2nd line in English\tC'est la deuxième ligne en Fran?ais
This is a the third line in English\tC'est la troisième ligne en Fran?ais
Currently, i can use this but saving a few million lines in the RAM will kill my machine.
目前,我可以使用它,但在 RAM 中保存几百万行会杀死我的机器。
String english = "/home/path-to-file/english";
String french = "/home/path-to-file/french";
BufferedReader enBr = new BufferedReader(new FileReader(english));
BufferedReader frBr = new BufferedReader(new FileReader(french));
ArrayList<String> enFile = new ArrayList<String>();
while ((line = enBr.readLine()) != null) {
enFile.add(line);
}
int index = 0;
while ((line = frBr.readLine()) != null) {
String enSentence = enFile.get(index);
System.out.println(line + "\t" + enSentence);
index++;
}
回答by aioobe
Put the calls to nextLine
on both readers in the same loop:
将调用nextLine
放在同一个循环中的两个读取器上:
String english = "/home/path-to-file/english";
String french = "/home/path-to-file/french";
BufferedReader enBr = new BufferedReader(new FileReader(english));
BufferedReader frBr = new BufferedReader(new FileReader(french));
while (true) {
String partOne = enBr.readLine();
String partTwo = frBr.readLine();
if (partOne == null || partTwo == null)
break;
System.out.println(partOne + "\t" + partTwo);
}
回答by npinti
This is how I would do it:
这就是我将如何做到的:
List<String> strings = new ArrayList<String>();
BufferedReader enBr = ...
BufferedReader frBr = ...
String english = "";
String french = "";
while (((english = enBr.readline()) != null) && ((french = frBr.readline) != null))
{
strings.add(english + "\t" + french);
}