Skip to content

Instantly share code, notes, and snippets.

@cstrap
Last active August 29, 2015 14:04
Show Gist options
  • Save cstrap/6126579987a2c4812589 to your computer and use it in GitHub Desktop.
Save cstrap/6126579987a2c4812589 to your computer and use it in GitHub Desktop.
Merge a list of files into another one
/**
* Merge a list of files into another one
*
* @param files
* @param mergedFile
* @throws IOException
*/
public static void mergeFiles(List<File> files, File mergedFile) {
if (mergedFile.exists()) {
mergedFile.delete();
}
try {
FileWriter fstream = new FileWriter(mergedFile, true);
BufferedWriter out = new BufferedWriter(fstream);
for (File f : files) {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String bufferLine;
while ((bufferLine = in.readLine()) != null) {
out.write(bufferLine);
out.newLine();
}
in.close();
}
out.close();
} catch (IOException e) {
err.println(e.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment