Read two textfile line by line simultaneously -java

alvas picture alvas · May 31, 2012 · Viewed 13.7k times · Source

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

Answer

aioobe picture aioobe · May 31, 2012

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