Last active
November 6, 2015 21:18
-
-
Save deshaion/9a4257c6fbaecd759342 to your computer and use it in GitHub Desktop.
Merge files to one qoutations
This file contains 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
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class Main { | |
public static void main(String[] args) { | |
String FilePath = "C:\\Users\\Ivan\\Downloads\\book"; | |
File folder = new File(FilePath); | |
File mergedFile = new File(FilePath + "\\quotations.txt"); | |
File[] files = folder.listFiles(); | |
Arrays.sort(files, new Comparator<File>() { | |
@Override | |
public int compare(File o1, File o2) { | |
return -1 * o1.getName().compareTo(o2.getName()); | |
} | |
}); | |
mergeFiles(files, mergedFile); | |
} | |
public static void mergeFiles(File[] files, File mergedFile) { | |
FileWriter fstream = null; | |
BufferedWriter out = null; | |
try { | |
fstream = new FileWriter(mergedFile, true); | |
out = new BufferedWriter(fstream); | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} | |
for (File f : files) { | |
System.out.println("merging: " + f.getName()); | |
FileInputStream fis; | |
try { | |
fis = new FileInputStream(f); | |
BufferedReader in = new BufferedReader(new InputStreamReader(fis)); | |
String aLine; | |
while ((aLine = in.readLine()) != null) { | |
out.write(aLine); | |
out.newLine(); | |
} | |
in.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
try { | |
out.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment