Java program to merge contents of all the files in a directory
Prerequisite : PrintWriter, BufferedReader.
We are given a directory/folder in which n number of files are stored(We dont know the number of files) and we want to merge the contents of all the files into a single file lets say output.txt
For the below example lets say the folder is stored at the path: F:\GeeksForGeeks
Following are the steps:
- Create instance of directory.
- Create a PrintWriter object for “output.txt”.
- Get list of all the files in form of String Array.
- Loop for reading the contents of all the files in the directory GeeksForGeeks.
- Inside the loop for every file do
-
- Create instance of file from Name of the file stored in string Array.
- Create object of BufferedReader for reading from current file.
- Read from current file.
- Write to the output file.
// Java program to merge all files of a directory import java.io.*; class sample { public static void main(String[] args) throws IOException { // create instance of directory File dir = new File("F:\\GeeksForGeeks"); // create obejct of PrintWriter for output file PrintWriter pw = new PrintWriter("output.txt"); // Get list of all the files in form of String Array String[] fileNames = dir.list(); // loop for reading the contents of all the files // in the directory GeeksForGeeks for (String fileName : fileNames) { System.out.println("Reading from " + fileName); // create instance of file from Name of // the file stored in string Array File f = new File(dir, fileName); // create object of BufferedReader BufferedReader br = new BufferedReader(new FileReader(f)); pw.println("Contents of file " + fileName); // Read from current file String line = br.readLine(); while (line != null) { // write to the output file pw.println(line); line = br.readLine(); } pw.flush(); } System.out.println("Reading from all files" + " in directory " + dir.getName() + " Completed"); } } |
Contents of folder F\GeeksForGeeks

Contents of 3 files in GeeksForGeeks folder:



Output file:

Recommended Posts:
- Java program to List all files in a directory and nested sub-directories | Recursive approach
- Java program to merge two files into a third file
- Java program to merge two files alternatively into third file
- Java program to merge two files alternatively into third file
- How to include() all PHP files from a directory ?
- Java Program for Merge Sort
- Java Program for Iterative Merge Sort
- Java Program for Merge Sort for Linked Lists
- Java Program to Count Inversions in an array | Set 1 (Using Merge Sort)
- Program for longest common directory path
- Traversing directory in Java using BFS
- Moving a file from one directory to another using Java
- How to run java class file which is in different directory?
- Jar files in Java
- Working with JAR and Manifest files In Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



