In this section,you will learn how to list files and folders present in the specified directory.
Ads
TutorialsThis example illustrates how to list files and
folders present in the specified directory. This topic is related to the
I/O
(input/output) of java.io
package.
In this example we are using File class of java.io package. The File class is an abstract representation of file and directory pathnames. This class is an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:
"/"
for the
UNIX root directory, or "\\"
for a Win32 UNC
pathname, andExplanation
This program list the file of the specified directory. We will be declaring a function called dirlist which lists the contents present in the specified directory.
dirlist(String fname)
The function dirlist(String fname) takes directory name as parameter. The function creates a new File instance for the directory name passed as parameter
File dir = new File(fname);
and retrieves the list of all the files and folders present in the directory by calling list() method on it.
String[] chld = dir.list();
Then it prints the name of files and folders present in the directory.
Code of the Program :
import java.io.*;
|
Download Directry Listing Example
Posted on: June 4, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles
Advertisements
Ads
Ads
Discuss: Java Directory - Directory and File Listing Example in Java View All Comments
Post your Comment