DirList.java
1. package futils;
2. import java.awt.*;
3. import java.io.*;
4. import java.util.*;
5.
6.
7. public class DirList {
8. String startDir;
9. private Vector history = new Vector();
10. int totalBytes = 0;
11. int totalFiles = 0;
12.
13. public static void main(String args[]) {
14. DirList dl = new DirList();
15. dl.print();
16. dl.printStats();
17. }
18.
19. DirList() {
20. startDir = Ls.getDirName();
21. startAtThisDir(startDir);
22. }
23. public File [] getFiles() {
24. File f[] = new File[history.size()];
25. for (int i=0; i < f.length; i++)
26. f[i] = (File)history.elementAt(i);
27. return f;
28. }
29. public void print() {
30. File f[] = getFiles();
31. for (int i=0; i < f.length; i++)
32. System.out.println(f[i]);
33.
34. }
35.
36. public void printStats() {
37. System.out.println("Saw " +
38. totalFiles +
39. " files with a total size of " +
40. totalBytes +
41. " bytes");
42. }
43.
44. //-------------------------------------------------------
45. // Recursive function that given an anchor directory
46. // will walk directory tree
47. //
48. //-------------------------------------------------------
49. public void startAtThisDir(String anchorDir)
50. {
51. FileFilter files = new FileFilter();
52. DirFilter dirs = new DirFilter();
53. File f1 = new File(anchorDir);
54. File f2;
55. FileInputStream fis;
56. int i;
57. String[] ls;
58. int bytes;
59.
60. //-------------------------------------------------------
61. // Loop through all the filenames in the current directory
62. // and store them in history:
63. //-------------------------------------------------------
64. for (ls = f1.list(files), i=0;
65. ls != null && i < ls.length; i++) {
66. totalFiles++;
67. f2 = new File(f1, ls[i]);
68. bytes = Futil.available(f2);
69. totalBytes += bytes;
70. history.addElement(f2);
71. }
72.
73. //-------------------------------------------------------
74. // This loop recurses on all directory names in
75. // the current working directory.
76. //-------------------------------------------------------
77. for (ls = f1.list(dirs),
78. i=0; ls != null && i < ls.length; i++)
79. startAtThisDir(anchorDir + ls[i] + f1.separator);
80. }
81.
82. }
83.
84.
85.