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


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

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

Latest Searches:
Eclipse 3.4 EJB
HTML .htaccess Rewriti
online
ASP Ad Management Rota
msaccess
count
ado
printwriter
sร?ยà¸?ร???ร?à
selected index
imput
checkLogin
jsf action form
connection class
jar entry
Mouse Event codes
asp.net2.0
JSON AJAX
print values from tabl
html:checkbox is check
d=d^2-4ac
heap
convert java.sql.Time
displaytag
Javascript Menus Image
date to calendar
how to scale image usi
tomcat database conf
analog
TimeTextBox
what is cckies
get array in java usin
exit
java strings
how to access https
ChartDirector Chart an
userid
login jsp
myfaces tomahawk
radio button in html
Flash Dynamic Content
à¹?à¸?à¸?à¹?à¸????à¹?à
how to draw a transpar
NamedQuery
java thread
javascript to display
struts2
Hash Function in Java
java beans
sฦ�ing
struttutorial
iterator
sybase create command
creating tabs
inset the value into t
decimal
update jsf
Combattons la programm
breakÑ?â??â??Ñ??Ñ?â???
Jasper Reports
How to restore page vi
validate radio button
form capture
gridback
jbossweb installation
shapes in applet
Visual Basic Animation
word count in a file
populate drop down
html:frame
string programe in jav
how to get applicate s
jpf
beans
PHP Date and .....:/pw
SimplyMEPIS 3.4-3 is r
varStatus
value change listener
program to invoke ejb
java arrays
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.