Create an array (or, if you prefer, a Collection) of Readers/InputStreams, one for each CSV file.
Similar to @JustinKSU idea, create a TreeMap, where the key is one line from the CSV file. Pass a custom Comparator, your custom impl that sorts by long, Date etc. The value is the index (probably an Integer, could be the filename if your Collection is a Map) of which file in your array/Collection.
Seed the TreeMap by reading the first line from each file.
Remove the lowest line using TreeMap.pollFirstEntry(), and write the key (the line) to a Writer/OutputStream. Use the value to read one more line from the appropriate file (checking for EOF) and add that into the TreeMap.
Repeat until TreeMap is empty. Close everything.
Edit - Added Source Code below
And Note, this only works if the input files are already sorted! (As was specified in the question)
public void mergeSort(File[] inFiles, File outFile, Comparator<String> comparator) throws IOException {
try {
BufferedReader[] readers = new BufferedReader[inFiles.length];
PrintWriter writer = new PrintWriter(outFile);
TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>(
comparator);
// read first line of each file. We don't check for EOF here, probably should
for (int i = 0; i < inFiles.length; i++) {
readers[i] = new BufferedReader(new FileReader(inFiles[i]));
String line = readers[i].readLine();
treeMap.put(line, Integer.valueOf(i));
}
while (!treeMap.isEmpty()) {
Map.Entry<String, Integer> nextToGo = treeMap.pollFirstEntry();
int fileIndex = nextToGo.getValue().intValue();
writer.println(nextToGo.getKey());
String line = readers[fileIndex].readLine();
if (line != null)
treeMap.put(line, Integer.valueOf(fileIndex));
}
}
finally {
// close everything here...
}
}