Home > Java > javaTutorial > body text

How to read all files in a folder into a single file using Java?

王林
Release: 2023-08-27 12:01:13
forward
1339 people have browsed it

The

listFiles()How to read all files in a folder into a single file using Java? method of the

File class returns an array holding the current (file) object.

Read the contents of all files in a folder into a single file -

  • By passing as argument the file path required to create the file object.
  • Read the contents of each file using Scanner or any other reader.
  • Append the read content to the StringBuffer.
  • Write the StringBuffer contents to the desired output file.

Example

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test {
   public static void main(String args[]) throws IOException {
      //Creating a File object for directory
      File directoryPath = new File("D:\SampleDirectory");
      //List of all files and directories
      File filesList[] = directoryPath.listFiles();
       System.out.println("List of files and directories in the specified directory:");
      Scanner sc = null;
      StringBuffer sb = new StringBuffer();
      for(File file : filesList) {
         System.out.println("File name: "+file.getName());
         System.out.println("File path: "+file.getAbsolutePath());
         System.out.println("Size :"+file.getTotalSpace());
         //Instantiating the Scanner class
         sc= new Scanner(file);
         String input;
         while (sc.hasNextLine()) {
            input = sc.nextLine();
            sb.append(input+" ");
         }
         System.out.println("Contents of the file: "+sb.toString());
         System.out.println(" ");        
           //Instantiating the FileOutputStream class
         FileOutputStream fileOut = new FileOutputStream("D:\output.txt");
         //Instantiating the DataOutputStream class
         DataOutputStream outputStream = new DataOutputStream(fileOut);
         //Writing UTF data to the output stream
         outputStream.write(sb.toString().getBytes());
         outputStream.flush();
         System.out.println("Data entered into the file");
      }
   }
}
Copy after login

Output

List of files and directories in the specified directory:
File name: sample1.txt
File path: D:\SampleDirectory\sample1.txt
Contents of the file: sample text file1

Data entered into the file
File name: sample2.txt
File path: D:\SampleDirectory\sample2.txt
Contents of the file: sample text file2

Data entered into the file
File name: sample3.txt
File path: D:\SampleDirectory\sample3.txt
Contents of the file: sample text file3

Data entered into the file
Copy after login

The above is the detailed content of How to read all files in a folder into a single file using Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template