Last active
August 29, 2015 14:04
-
-
Save cstrap/6126579987a2c4812589 to your computer and use it in GitHub Desktop.
Merge a list of files into another one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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