Open In App

Java program to merge contents of all the files in a directory

Last Updated : 25 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 
 

  1. Create instance of directory.
  2. Create a PrintWriter object for “output.txt”.
  3. Get list of all the files in form of String Array.
  4. Loop for reading the contents of all the files in the directory GeeksForGeeks.
  5. Inside the loop for every file do
    1. Create instance of file from Name of the file stored in string Array.
    2. Create object of BufferedReader for reading from current file.
    3. Read from current file.
    4. Write to the output file.

 

Java




// 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 object 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:
 

 



Similar Reads

Java Program to List all Files in a Directory and Nested Sub-Directories
Prerequisites: File class Given a main directory/folder, list all the files from it, and if this directory has other nested sub-directories, list files from them. It is pretty easy to observe a simple recursion pattern in the above problem. Algorithm : Create a File object for the main directory.Get an array of files for the main directory.If array
4 min read
How to Scan All Mp3 Files In the Given Directory using Java?
In this article, we will discuss how to scan all mp3 files and not only from a specific path for an android media player. We will use a recursive method for scanning files. Scan all folders and sort out all mp3 files using the File and FileFilter class. Implementation Java Code import java.io.File; import java.io.FileFilter; // Java program for sca
1 min read
How to List all Files in a Directory in Java?
Java provides a feature to interact with the file system through the java.io.file package. This feature helps developers automate the things to operate with files and directories. In this article, we will learn how to list all the files in a directory in Java. Approaches to list all files in a directory We have two ways to get the list of all the f
3 min read
List all Files from a Directory Recursively in Java
In Java, when working with file systems, sometimes it is necessary to list all files from a directory, including those within its subdirectories. This process, known as recursive directory traversal, allows us to explore the entire hierarchy of files and folders starting from a given directory. Java provides classes like File and Path to handle fil
3 min read
How to find and open the Hidden files in a Directory using Java
Pre-requisites: Java File Handling So far the operations using Java programs are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. In this article, we will see how to ope
3 min read
Java program to merge two files alternatively into third file
Prerequisite : PrintWriter , BufferedReader Let the given two files be file1.txt and file2.txt. Our Task is to merge both files into third file say file3.txt but merging should be done by line by line alternatively. The following are steps to merge alternatively. Create PrintWriter object for file3.txt Open BufferedReader for file1.txt Open Buffere
2 min read
Java program to merge two files into a third file
Prerequisite : PrintWriter , BufferedReader Let the given two files be file1.txt and file2.txt. Our Task is to merge both files into third file say file3.txt. The following are steps to merge. Create PrintWriter object for file3.txt Open BufferedReader for file1.txt Run a loop to copy each line of file1.txt to file3.txt Open BufferedReader for file
2 min read
Java program to merge two files alternatively into third file
Prerequisite : PrintWriter , BufferedReader Let the given two files be file1.txt and file2.txt. Our Task is to merge both files into third file say file3.txt but merging should be done by line by line alternatively. The following are steps to merge alternatively. Create PrintWriter object for file3.txt Open BufferedReader for file1.txt Open Buffere
2 min read
Java Program to Create String from Contents of a File
A File is a computer resource which is deployed to store different types of data such as text, image, video, to name a few. It is basically a collection of data bound to a single entity. While using your computer, it becomes essential to be able to deal with files and in this article we will be learning about various ways to read data from files. W
6 min read
Java Program to Retrieve Contents of a Table Using JDBC connection
It can be of two types namely structural and non-structural database. The structural database is the one which can be stored in row and columns. A nonstructural database can not be stored in form of rows and columns for which new concepts are introduced which would not be discussing here. Most of the real-world data is non-structural like photos, v
5 min read
Practice Tags :