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.
Contents of folder F\GeeksForGeeks
Contents of 3 files in GeeksForGeeks folder:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.