
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to read all files in a folder to a single file using Java?
The listFiles() method of the File class returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.
To read the contents of all the files in a folder into a single file −
- Create a file object by passing the required file path as a parameter.
- Read the contents of each file using Scanner or any other reader.
- Append the read contents into a StringBuffer.
- Write the StringBuffer contents into the required 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"); } } }
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
- Related Articles
- Python - Read all CSV files in a folder in Pandas?
- How to read data from all files in a directory using Java?
- How to get list of all files/folders from a folder in Java?
- How to read multiple text files from a folder in Python?(Tkinter)
- Python - How to Merge all excel files in a folder
- Ask a user to select a folder to read the files in Python
- How to read a single character using Scanner class in Java?
- How to obtain a list of all files in a public folder in Laravel?
- How to read data in from a file to String using java?
- How to read integers from a file using BufferedReader in Java?
- How to list all files in a directory using Java?
- How to read the contents of a JSON file using Java?
- Golang program to read and print all files from zip file
- How to read a single file inside a zip archive with PHP
- How to find all the distinct file extensions in a folder hierarchy (Linux)?

Advertisements