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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Traversing a file and directory under a directory 
 

This java tutorial will help you to traverse the file and directory under a directory.

 

Traversing  files and directories

                         

This section you will learn how the files and subdirectories are traversed lying under a directory. We can find that, how many files and directories are available within a directory. This is very frequently used where lot of work depends on the files and directories. .

The given program describes how you can traverse a directory if it contains directory and files under it. If the directory contains no files and directory then it will tell you that the directory contains no directory and files in it, if it contains a directory and files in it then it will display all the files and directories.
To implement this program, first make a class named as TraversingFileAndDirectories then make an object of File class and pass any directory and filename to its constructor. After that, check it whether the file or directory you have entered is a directory or a file. If it is a directory then it will display that it is a directory otherwise not a directory. If it is a directory, it checks whether it contains more directory and files in it. If it contains then it will display you the list of the files and directories.

Code of the program is given below

import java.io.*;


 
public class TraversingFileAndDirectories{
 
 
public static void main(String args[]){
  
  
String dirname = args[0];
 
    
File file = new File(dirname)
 
        
if(file.isDirectory()){
 
      
System.out.println("Directory is  " + dirname);
   
   
String str[] = file.list();
 
      
forint i = 0; i<str.length; i++){
  
      
File f=new File(dirname + " / " + str[i]);
  
      
if(f.isDirectory()){
   
       
System.out.println(str[i" is a directory");
 
        
}
 
       
else{
 
         
System.out.println(str[i" is a file");
 
        
}
 
      
}
 
    
}
  
  
else{
 
      
System.out.println(dirname  + " is not a directory");
 
    
}

  }

}


Output of the program 

C:\java>java TraversingFileAndDirectories chandan
Directory is chandan
ConstructingFileNamePath.java is a file
constructingfilenamepath.shtml is a file
EnterValuesFromKeyboard.java is a file
entervaluesfromkeyboard.shtml is a file
Factorial.java is a file
factorial.shtml is a file
UseOfThisOperator.java is a file
useofthisoperator.shtml is a file

Download this program

                         

» View all related tutorials
Related Tags: c orm form time script object io objects help method sed system ip collection this opera create show for work

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:

this program tells how many file and directories are ther in u r parent directory

Posted by venkat on Sunday, 12.9.07 @ 12:33pm | #41642

this program tells how many file and directories are ther in u r parent directory

Posted by venkat on Sunday, 12.9.07 @ 12:32pm | #41641

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.