Reading from two files parallel in Java
Recently I helped someone at Wizpert who wanted to read from two filers parallel — and do something with the data read (for example merging the two files into one line-by-line).
And I think this would be a good thing to share my wisdom with you here too.
Let’s stay with the example above. Imagine, you have to read two files parallel and merge their content into a third file without storing the contents in temporal objects for example lists.
It would look something like this:
file1 line 1 | file2 line1 file1 line2 | file2 line2 ... file1 line n | file2 line n
If one of the files has more entries than the other then you have to leave the part of the other file empty, but the pipe must be there.
This would look like this for the case when file 1 is longer:
file1 line m | file2 line m file1 line m+1 | file1 line m+2 | ... file1 line n |
And the other case where the second file is longer:
file1 line m | file2 line m | file2 line m+1 | file2 line m+2 ... | file2 line n
You would do something like this to read the lines:
while ((line1 = br1.readLine()) != null || (line2 = br2.readLine()) != null)
However if you try to run your code you will see, that this does not really work.
This is because the “or” operator: it is short-circuit: if the first part evaluates to true, it does not execute the second part (or even any further).
This means, that in this case all the lines of file 1 (through the BufferedReader br1) are read but none of file 2 until br1 returns any more lines.
And by the way, Eclipse (where I created the sample application) brags about that “line2 may not have been initialized” — so a good IDE gets the problem, however you can go around this problem easily.
So to solve this problem use the not-short-circuit version of the “or” operator:
while ((line1 = br1.readLine()) != null | (line2 = br2.readLine()) != null)
Another solution would be to read lines from both files in an endless loop for example and the break condition would be if both lines are null.
There are many options you could implement to handle this particular problem. I do not say which one to use, it always depends on your taste. However if the result is not what you expected, in this case the error could be in the implementation so let another pair of eyes have a look at your code to find the error easier.
I prepared some example code to let you see a complete solution of the problem. I shared the sources at GitHub.
And one more thing: do not forget to trim() your lines to do not have strange line-breaks in your resulting file. It can happen sometimes 🙂