Java Reference
In-Depth Information
different full pathnames. A directory in the Unix file system is just a file with
a list of all its children, 1 so the directories can be traversed with an iteration
scheme; that is, we can sequentially iterate over each child. Indeed, on some
systems, if the normal command to print a file is applied to a directory, the
filenames in the directory appear in the output (along with other non-ASCII
information).
Suppose that we want to list the names of all the files in a directory (including
its subdirectories), and in our output format files of depth d have their names
indented by d tab characters. A short algorithm to do this task is given in
Figure 18.5. Output for the directory presented in Figure 18.4 is shown in
Figure 18.6.
The directory struc-
ture is most easily
traversed by using
recursion.
1 void listAll( int depth = 0 ) // depth is initially 0
2 {
3 printName( depth ); // Print the name of the object
4 if( isDirectory( ) )
5 for each file c in this directory (for each child)
6 c.listAll( depth + 1 );
7
figure 18.5
A routine for listing a
directory and its
subdirectories in a
hierarchical file
system
}
mark
books
dsaa
ch1
ch2
ecp
ch1
ch2
ipps
ch1
ch2
courses
cop3223
syl
cop3530
syl
.login
figure 18.6
The directory listing
for the tree shown in
Figure 18.4
1. Each directory in the Unix file system also has one entry ( . ) that points to itself and another
entry ( .. ) that points to the parent of the directory, which introduces a cycle. Thus, techni-
cally, the Unix file system is not a tree but is treelike. The same is true for Windows/DOS.
 
Search WWH ::




Custom Search