- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read data from all files in a directory using Java?
The class named File of the java.io package represents a file or directory (pathnames) in the system. This class provides various methods to perform various operations on files/directories.
To get the list of all the existing files in a directory this class provides five different methods to get the details of all files in a particular folder −
- String[] list()
- File[] listFiles()
- String[] list(FilenameFilter filter)
- File[] listFiles(FilenameFilter filter)
- File[] listFiles(FileFilter filter)
The ListFiles() method
This method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.
The following Java program prints the name, path and, size of all the files in the path D:\ExampleDirectory.
Example
import java.io.File; import java.io.IOException; public class ListOfFiles { public static void main(String args[]) throws IOException { //Creating a File object for directory File directoryPath = new File("D:\ExampleDirectory"); //List of all files and directories File filesList[] = directoryPath.listFiles(); System.out.println("List of files and directories in the specified directory:"); for(File file : filesList) { System.out.println("File name: "+file.getName()); System.out.println("File path: "+file.getAbsolutePath()); System.out.println("Size :"+file.getTotalSpace()); System.out.println(" "); } } }
Output
List of files and directories in the specified directory: File name: SampleDirectory1 File path: D:\ExampleDirectory\SampleDirectory1 Size :262538260480 File name: SampleDirectory2 File path: D:\ExampleDirectory\SampleDirectory2 Size :262538260480 File name: SampleFile1.txt File path: D:\ExampleDirectory\SampleFile1.txt Size :262538260480 File name: SampleFile2.txt File path: D:\ExampleDirectory\SampleFile2.txt Size :262538260480 File name: SapmleFile3.txt File path: D:\ExampleDirectory\SapmleFile3.txt Size :262538260480
Reading contents of all files in a directory
To read the contents of all files in a particular directory, get the File objects of all files in it as an array, using the above-mentioned method and then, read contents of each file object in the array using the Scanner class and its methods.
Example
import java.io.File; import java.io.IOException; import java.util.Scanner; public class ListOfFiles { public static void main(String args[]) throws IOException { //Creating a File object for directory File directoryPath = new File("D:\Demo"); //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; 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; StringBuffer sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(input+" "); } System.out.println("Contents of the file: "+sb.toString()); System.out.println(" "); } } }
Output
List of files and directories in the specified directory: File name: samplefile1.txt File path: D:\Demo\samplefile1.txt Size :262538260480 Contents of the file: Contents of the sample file 1 File name: samplefile2.txt File path: D:\Demo\samplefile2.txt Size :262538260480 Contents of the file: Contents of the sample file 2 File name: samplefile3.txt File path: D:\Demo\samplefile3.txt Size :262538260480 Contents of the file: Contents of the sample file 3 File name: samplefile4.txt File path: D:\Demo\samplefile4.txt Size :262538260480 Contents of the file: Contents of the sample file 4 File name: samplefile5.txt File path: D:\Demo\samplefile5.txt Size :262538260480 Contents of the file: Contents of the sample file 5
- Related Articles
- How to list all files (only) from a directory using Java?
- How to list all files in a directory using Java?
- How to read all excel files under a directory as a Pandas DataFrame ?
- How to read all files in a folder to a single file using Java?
- Java program to List all files in a directory recursively
- Java program to delete all the files in a directory recursively (only files)
- How to read data in from a file to String using java?
- How to list down all the files available in a directory using C#?
- Java program to List all files in a directory and nested sub-directory - Recursive approach
- How to delete all files in a directory with Python?
- How to unzip all zipped files in a Linux directory?
- Java program to merge contents of all the files in a directory
- How to list out the hidden files in a Directory using Java program?
- How to perform grep operation on all files in a directory?
- How to read data from user using the Console class in Java?
