Java Read Each File in a Directory

How to go list of all files/folders from a folder in Java?


The class named File of the java.io bundle represents a file or directory (path names) in the system. This class provides diverse methods to perform diverse operations on files/directories.

To get the list of all the existing files in a directory this class provides the files class provides list() (returns names) and ListFiles (returns File objects) with different variants.

The Listing() method

This method returns a String array which contains the names of all the files and directories in the path represented past the current (File) object.

Using this method, yous tin can just impress the names of the files and directories.

Case

The following Java programme lists the names of all the files and directories in the path D:\\ExampleDirectory.

import java.io.File; import coffee.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       Cord contents[] = directoryPath.list();       Organization.out.println("Listing of files and directories in the specified directory:");       for(int i=0; i<contents.length; i++) {          System.out.println(contents[i]);       }    } }

Output

List of files and directories in the specified directory: SampleDirectory1 SampleDirectory2 SampleFile1.txt SampleFile2.txt SapmleFile3.txt

The ListFiles() method

This method returns an array holding the objects (abstruse paths) of all the files (and directories) in the path represented by the electric current (File) object.

Since this method returns the objects of each file/directory in a binder. Using it you lot can access the properties of the files/directories such as size, path etc.

Example

The following Coffee program prints the name, path and, size of all the files in the path D:\\ExampleDirectory.

import coffee.io.File; import java.io.IOException; public class ListOfFiles {    public static void principal(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());          Organisation.out.println(" ");       }    } }

Output

List of files and directories in the specified directory: File proper noun: SampleDirectory1 File path: D:\ExampleDirectory\SampleDirectory1 Size :262538260480  File name: SampleDirectory2 File path: D:\ExampleDirectory\SampleDirectory2 Size :262538260480  File proper name: SampleFile1.txt File path: D:\ExampleDirectory\SampleFile1.txt Size :262538260480  File name: SampleFile2.txt File path: D:\ExampleDirectory\SampleFile2.txt Size :262538260480  File proper name: SapmleFile3.txt File path: D:\ExampleDirectory\SapmleFile3.txt Size :262538260480

The List(FilenameFilter filter) method

As suggested in its signature, this method accepts a FilenameFilter object and returns a String array containing the names of all the files and directories in the path represented by the electric current (File) object. But the retuned array contains the filenames which are filtered based on the specified filter.

Using this method, you lot tin can get the filtered names of the files and directories in a detail folder.

Case

The following Java program prints the names of the text files in the path D:\\ExampleDirectory.

import java.io.File; import coffee.io.FilenameFilter; 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");       FilenameFilter textFilefilter = new FilenameFilter(){          public boolean have(File dir, String name) {             String lowercaseName = name.toLowerCase();             if (lowercaseName.endsWith(".txt")) {                return true;             } else {                return false;             }          }       };       //List of all the text files       String filesList[] = directoryPath.list(textFilefilter);       Organisation.out.println("List of the text files in the specified directory:");       for(String fileName : filesList) {          Arrangement.out.println(fileName);       }    } }

Output

Listing of the text files in the specified directory −

SampleFile1.txt SampleFile2.txt SapmleFile3.txt

The ListFiles(FilenameFilter filter) method

This method accepts a FilenameFilter object and returns a File assortment containing the objects of all the files and directories in the path represented past the current File object. But the retuned array contains the files (objects) which are filtered based on their name

Using this method, you can become the filtered file objects of the files and directories in a detail binder, according to their names.

Case

The following Coffee program prints the name, path and, size of all the text files in the path D:\\ExampleDirectory.

import java.io.File; import coffee.io.FilenameFilter; import java.io.IOException; public class ListOfFiles {    public static void main(Cord args[]) throws IOException {       //Creating a File object for directory       File directoryPath = new File("D:\\ExampleDirectory");       FilenameFilter textFilefilter = new FilenameFilter(){          public boolean accept(File dir, String proper name) {             String lowercaseName = proper noun.toLowerCase();             if (lowercaseName.endsWith(".txt")) {                return truthful;             } else {                return imitation;             }          }       };       //List of all the text files       File filesList[] = directoryPath.listFiles(textFilefilter);       System.out.println("List of the text files in the specified directory:");       for(File file : filesList) {          System.out.println("File name: "+file.getName());          System.out.println("File path: "+file.getAbsolutePath());          Arrangement.out.println("Size :"+file.getTotalSpace());          System.out.println(" ");       }    } }

Output

List of the text files in the specified directory: File proper noun: SampleFile1.txt File path: D:\ExampleDirectory\SampleFile1.txt Size :262538260480  File proper name: SampleFile2.txt File path: D:\ExampleDirectory\SampleFile2.txt Size :262538260480  File name: SapmleFile3.txt File path: D:\ExampleDirectory\SapmleFile3.txt Size :262538260480

The ListFiles(FileFilter filter) method

This method accepts a FileFilter object and returns a File array containing the (File) objects of all the files and directories in the path represented past the current File object. Only the retuned array contains the files (objects) which are filtered based on the property of the file.

Using this method, yous can go the filtered file objects of the files and directories in a item folder based on the size, path, blazon (file or, directory) etc…

Instance

The following Coffee plan prints the name, path and, size of all the files (not folders) in the path D:\\ExampleDirectory.

import java.io.File; import java.io.FileFilter; import java.io.IOException; public course ListOfFiles {    public static void main(String args[]) throws IOException {       //Creating a File object for directory       File directoryPath = new File("D:\\ExampleDirectory");       FileFilter textFilefilter = new FileFilter(){          public boolean accept(File file) {             boolean isFile = file.isFile();             if (isFile) {                render true;             } else {                return false;             }          }       };       //Listing of all the text files       File filesList[] = directoryPath.listFiles(textFilefilter);       Organisation.out.println("List of the text files in the specified directory:");       for(File file : filesList) {          System.out.println("File proper noun: "+file.getName());          System.out.println("File path: "+file.getAbsolutePath());          Organisation.out.println("Size :"+file.getTotalSpace());          System.out.println(" ");       }    } }

Output

List of the text files in the specified directory: File name: cassandra_logo.jpg File path: D:\ExampleDirectory\cassandra_logo.jpg Size :262538260480  File proper name: cat.jpg File path: D:\ExampleDirectory\cat.jpg Size :262538260480  File proper name: coffeescript_logo.jpg File path: D:\ExampleDirectory\coffeescript_logo.jpg Size :262538260480  File name: javafx_logo.jpg File path: D:\ExampleDirectory\javafx_logo.jpg Size :262538260480  File proper 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

raja

Published on 02-Aug-2019 11:01:52

  • Related Questions & Answers
  • How to delete all files and folders from a path in C#?
  • How to brandish files/folders including hidden files/folders in PowerShell?
  • How to delete folder and sub folders using Coffee?
  • How to listing all files (only) from a directory using Java?
  • Python - How to Merge all excel files in a folder
  • How to read all files in a binder to a unmarried file using Coffee?
  • How to become hidden files and folders using PowerShell?
  • Get all the images from a folder in PHP
  • How to get only hidden files and folders in PowerShell?
  • Write a C program to print all files and folders.
  • How to list all files in a directory using Java?
  • How to become the list of shared folders using PowerShell?
  • How to go the list of empty folders using PowerShell?
  • How to get the directories (only) from a binder using Java?
  • Python - Read all CSV files in a folder in Pandas?

blevinspland1955.blogspot.com

Source: https://www.tutorialspoint.com/how-to-get-list-of-all-files-folders-from-a-folder-in-java

0 Response to "Java Read Each File in a Directory"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel