我有两个不同语言的文本文件,它们按行对齐。即文本文件1中的第一行应该等于文本文件2中的第一行,以此类推。
有没有一种方法可以同时逐行读取这两个文件?
下面是文件的样本,想象每个文件的行数大约为1000000。
文本文件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
文本文件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
期望的输出
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
目前,我可以使用这个方法,但将几百万行数据保存在内存中会使我的计算机几乎崩溃。
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++;
}
paste -d '\t' english french > englishandfrench
。 - Zakaria