I would like to merge csv files present in some folder into one file. Suppose there are 12000 files in folder and every file have 20000 record. Could any one please give me some best idea/solution using multi-threading concept. I have written below code but i think for large data it is not fine :
public class Test {
public static void main(String[] args) throws IOException {
String path="Desktop//Files//";
List<Path> listofFile=getListOfFileInFolder(path);
List<Path> paths = listofFile;
List<String> mergedLines = getMergedLines(paths);
Path target = Paths.get(path+"temp.csv");
System.out.println(target);
Files.write(target, mergedLines, Charset.forName("UTF-8")); }
public static List<Path> getListOfFileInFolder(String path){
List<Path> results = new ArrayList<Path>();
File[] files = new File(path).listFiles();
for (File file : files) {
if (file.isFile()) {
results.add(Paths.get(path+file.getName()));
}
}
return results;
}
private static List<String> getMergedLines(List<Path> paths) throws IOException {
List<String> mergedLines = new ArrayList<> ();
for (Path p : paths){
List<String> lines = Files.readAllLines(p, Charset.forName("UTF-8"));
if (!lines.isEmpty()) {
if (mergedLines.isEmpty()) {
mergedLines.add(lines.get(0)); //add header only once
}
mergedLines.addAll(lines.subList(1, lines.size()));
}
}
return mergedLines;
}
}