Home Java Beginners Java Directory - Directory and File Listing Example in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java Directory - Directory and File Listing Example in Java

Advertisement
In this section,you will learn how to list files and folders present in the specified directory.

Java Directory - Directory and File Listing Example in Java

     

This 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:

  1. An optional system-dependent prefix string,
    such as a disk-drive specifier, "/" for the UNIX root directory, or "\\" for a Win32 UNC pathname, and
  2. A sequence of zero or more string names.

Explanation

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.*;

public class  DirListing{
 
 private static void dirlist(String fname){
  File dir = new File(fname);
 
   String[] chld = dir.list();
  if(chld == null){
  System.out.println("Specified directory does not exist or is not a directory.");
  System.exit(0);
  
  }else{
  
for(int i = 0; i < chld.length; i++){
  String fileName = chld[i];
  System.out.println(fileName);
  }
  }
  }
  public static void main(String[] args){
  switch(args.length){
  case 0: System.out.println("Directory has not mentioned.");
  System.exit(0);
  case 1: dirlist(args[0]);
 
 System.exit(0);
  default : System.out.println("Multiple files are not allow.");
  
  System.exit(0);
  }
  }
}

Download Directry Listing Example

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Ask Questions?    Discuss: Java Directory - Directory and File Listing Example in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Anonymous
August 16, 2011
Java list file

Hi I am an IT student and we have an assignment to write a recursive program that will prompt the user to enter a path to any folder and print a list of files saved under the entered folder. I am strugling with placing the output on a dialog box please help....
jake
January 26, 2013
i need java for to run a game

i am happy
brennan
April 14, 2012
minecraft

why
jeremy
September 26, 2012
subject

i do"t have one
Vutri
October 26, 2012
OqNLfeSLtiTHULVSBjO

VP of Instant Messaging Freedom Corporation Awwwwww yeah!! By the way, I love Gaim Pidgin and used it religiously back when I was stuck with a Windows box.And I might be the only one, but I'm wntiiag with bated breath to see what's comin' out of the Meebo labs soon
DMCA.com