Read two textfile line by line simultaneously -java

时光总嘲笑我的痴心妄想 提交于 2019-12-07 00:04:27

问题


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.

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.

textfile1:

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:

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.

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++;
}

回答1:


Put the calls to nextLine on both readers in the same loop:

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);
}



回答2:


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);
}


来源:https://stackoverflow.com/questions/10831007/read-two-textfile-line-by-line-simultaneously-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!