Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can avoid the use of the Set as intermediate storage if you write at the same time that you read from file. You could do something like this,</p> <pre><code>import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; public class AppMain { private static final int NUMBER_REGISTERS = 10000000; private static String[] filePaths = {"filePath1", "filePaht2", "filePathN"}; private static String mergedFile = "mergedFile"; public static void main(String[] args) throws IOException { mergeFiles(filePaths, mergedFile); } private static void mergeFiles(String[] filePaths, String mergedFile) throws IOException{ BufferedReader[] readerArray = createReaderArray(filePaths); boolean[] closedReaderFlag = new boolean[readerArray.length]; PrintWriter writer = createWriter(mergedFile); int currentReaderIndex = 0; int numberLinesInMergedFile = 0; BufferedReader currentReader = null; String currentLine = null; while(numberLinesInMergedFile &lt; NUMBER_REGISTERS &amp;&amp; getNumberReaderClosed(closedReaderFlag) &lt; readerArray.length){ currentReaderIndex = (currentReaderIndex + 1) % readerArray.length; if(closedReaderFlag[currentReaderIndex]){ continue; } currentReader = readerArray[currentReaderIndex]; currentLine = currentReader.readLine(); if(currentLine == null){ currentReader.close(); closedReaderFlag[currentReaderIndex] = true; continue; } writer.println(currentLine); numberLinesInMergedFile++; } writer.close(); for(int index = 0; index &lt; readerArray.length; index++){ if(!closedReaderFlag[index]){ readerArray[index].close(); } } } private static BufferedReader[] createReaderArray(String[] filePaths) throws FileNotFoundException{ BufferedReader[] readerArray = new BufferedReader[filePaths.length]; for (int index = 0; index &lt; readerArray.length; index++) { readerArray[index] = createReader(filePaths[index]); } return readerArray; } private static BufferedReader createReader(String path) throws FileNotFoundException{ BufferedReader reader = new BufferedReader(new FileReader(path)); return reader; } private static PrintWriter createWriter(String path) throws FileNotFoundException{ PrintWriter writer = new PrintWriter(path); return writer; } private static int getNumberReaderClosed(boolean[] closedReaderFlag){ int count = 0; for (boolean currentFlag : closedReaderFlag) { if(currentFlag){ count++; } } return count; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload