1

I would like to read several files at the same time in Java. I have for example 5 files, and each file has 50 lines (but I cann't know it in advance).

I would like to read the line number 1 on each file, then the line number 2 on each file etc..

How can I do it ?

I have a BufferedReader array like :

BufferedReader[] readers = new BufferedReader[x]; 
MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42
iAmoric
  • 1,787
  • 3
  • 31
  • 64
  • You could use 5 threads. But if you do that to speed things up, all 5 files should be on different drives otherwise it's probably gonna be slower instead of faster. – Tesseract Apr 14 '17 at 20:02
  • I don't want to use threads.. Is it not possible to do this with for and while loops ? – iAmoric Apr 14 '17 at 20:04
  • Why do you need to read them at the same time? And how fast do you need to read them? – Tesseract Apr 14 '17 at 20:05
  • Fill your array with reader responsible for each file, then iterate over them and make them read one line. Repeat until there is no more lines to read. – Pshemo Apr 14 '17 at 20:06
  • @SpiderPig, I need to concatenate information of each file at the same line. The speed is not important. – iAmoric Apr 14 '17 at 20:09

4 Answers4

3

You may use a loop to iterate while you get a new line from the readers.
Inside the loop, add a nested loop to iterate over the readers. In this way, you can read the line matching to the same line number of each BufferedReader.

Here is the idea :

BufferedReader[] readers = new BufferedReader[5]; 
readers[0] = new BufferedReader(new FileReader("...));
...
readers[4] = new BufferedReader(new FileReader("...));


boolean noMoreLine = false;
while (!noMoreLine)       

   for (BufferedReader reader : readers){
        String line = reader.readLine();
        if (line == null){
           noMoreLine = true;
           System.out.println("no more line");
           break;
        }
        System.out.println("Line " + i + " = line);
       }
    }

}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

If I understand correctly, you have an array set up, with one buffered reader for each file you need to read. If you want to read the first line of every file, the the second of every file, etc., you need to stop when you've reached the last line of the longest file.

You can get this value by finding the number of lines in each file, and saving the maximum. Number of lines in a file in Java might be useful.

Then, write a for loop:

for (int i = 0; i < longestFileLength; i++) {
    String fileLine1 = readers[1].readLine();
    String fileLine2 = readers[2].readLine();
    //...
    String fileLine99 = readers[99].readLine();
}
Community
  • 1
  • 1
MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42
1

I'd use an ArrayList rather than an array.

ArrayList<BufferedReader> readers = new ArrayList<>();
...
while(!readers.isEmpty()) {
  for(BufferedReader reader: readers) {
    String line = reader.readLine();
    if(line == null) {
      readers.remove(reader);
    } else {
      ...
    }
  }
}
Tesseract
  • 8,049
  • 2
  • 20
  • 37
1

You could use streams:

while(Arrays.stream(readers).filter(br -> br.ready()).count()>0)
    Arrays.stream(readers).map(br -> br.readLine()).toArray()
jcaron
  • 17,302
  • 6
  • 32
  • 46
leoconco
  • 253
  • 3
  • 15