1

I have List of Paths, each path represents file with text lines - i.e. 10 lines in each file. My task is to read each first line from each file, then each second, each third and so on.

Trying to do this with BufferedReader so far results in it reading only first lines.

 List<String> eachNLineFromEachFile = new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            for (Path tempfile : tempFilesList) {
                BufferedReader tempfileReader = Files.newBufferedReader(tempfile);

                String line = null;
                line = tempfileReader.readLine();
                eachNLineFromEachFile.add(line);
                try (BufferedWriter outputwriter = Files.newBufferedWriter(outputFile)) {
                    outputwriter.write(String.valueOf(eachNLineFromEachFile));
                }

                System.out.println(eachNLineFromEachFile);

            }
        }

So far it results with reading each first line of each file and then repeating with each first line.

How can get needed offset to work? I.e. each loop iteration to start reading from next line. AFAIK I should not use RandomAccessFile, which could probably do the trick with its getFilePointer and seek() methods).

Please help and thanks in advance.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
eugene p
  • 9
  • 3
  • One possibility would be to read each file into a String[][] array. The first subscript represents the number of files and the second subscript represents the number of lines in each file. – Gilbert Le Blanc Dec 26 '20 at 14:46

2 Answers2

1

You could add readers into an array / list and loop through as shown below or as suggested by one of the users - copy the content into a [][] or a map with key as line number and do whatever you want post that ...

     List<String> eachNLineFromEachFile = new ArrayList<>();
     //dummy for test.
     List<Path> tempFilesList = new ArrayList<>();
     List<BufferedReader> readers = new ArrayList<>();
     for (Path tempfile : tempFilesList) {
         BufferedReader tempfileReader = Files.newBufferedReader(tempfile);
         readers.add(tempfileReader);
     }
     boolean read_complete = false;
     while(true) {
         for (BufferedReader reader : readers) {
             String line = null;
             try {
                line = reader.readLine();
                if(line == null) {
                    read_complete = true;
                    break;
                }
                eachNLineFromEachFile.add(line);
                System.out.println(eachNLineFromEachFile);
             } catch (IOException e) {
                e.printStackTrace();
            }
         }
         if(read_complete) {
             break;
         }
     }
vbn
  • 274
  • 2
  • 7
0

Reading your code I would expect the very same behavior as the one that you have described as problematic. You are just creating a new BufferedReader and a new BufferedWriter in each iteration, outputting a value. You would need instead to create an array of BufferedReader elements on the first iteration of the first cycle and repeat reading/writing the lines in the second cycle:

 BufferedWriter bf = Files.newBufferedWriter(outputFile);
 BufferedReader[] brs = new BufferedReader[10];
        for (int i = 0; i < 10; i++) {
            brs[i] = Files.newBufferedReader(tempFilesList.get(i));
        }
        for (int outerIndex = 0; outerIndex < 10; outerIndex++) {
            for (int innerIndex = 0; innerIndex < 10; innerIndex++) {
                String line = null;
                line = brs[innerIndex].readLine();
                bf.write(String.valueOf(line));
            }
        }
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175