![]() |
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 directoryWe have two ways to get the list of all the files in a directory in Java.
Program to list all files in a directory in JavaMethod 1: Using listFiles()In the java.io.File class which provides an inbuilt method listFiles() that will return an array of File objects of the files in the directory. Java
Output:Explanation of the Program:
Method 2: Using Java New I/O PackageNew I/O provides a new easy way to interact with Files and apply file operations. The Files and Path classes are available in the java.nio.file package which we can use to list the files in a directory. Java
Output:Explanation of the Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |