Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Java Directory - Directory and File Listing Example in Java 
 

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

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

private static void dirlist(String fname, int deep) {
File dir = new File(fname);
String[] chld = dir.list();
if (dir.isFile()) {
System.out.println("" + dir.getName());
return;

} else if (dir.isDirectory()) {
System.out.println(fname.substring(fname.lastIndexOf("\\")));
for (int i = 0; i < chld.length; i++) {
dirlist(fname + "\\" + chld[i]);
}
}
}

Posted by flo on Sunday, 12.7.08 @ 07:43am | #82482

its nice Code it is helpfull for bigners

Posted by Dildar ALi on Tuesday, 01.15.08 @ 14:37pm | #45250

This one is a real beauty. So simple the code and so powerful the function. Love it. Many thanks.

Posted by Kimtran on Wednesday, 06.13.07 @ 10:29am | #19083

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.