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
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
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles