Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
Java
  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

                         

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

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

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

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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