CS1103 Exercise Unit 6 - List all files in a directory and all its subdirectories recursively

Questions

Please write a program to list all files in a directory and all its subdirectories, and this program should ask the user to type in a directory name; if the name entered by the user is not a directory, the program ends with an error message printed out.

1. Codes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.File;
import java.util.Scanner;

/**
* This program lists all the files in a directory and all its subdirectories.
* The user is asked to type in a directory name.
* If the name entered by the user is not a directory, a message is printed and the program ends.
*/
public class DirectoryList {
public static void main(String[] args) {
String directoryName; // Directory name entered by the user.
File directory; // File object referring to the directory.
String[] files; // Array of file names in the directory.
Scanner scanner; // For reading a line of input from the user.

scanner = new Scanner(System.in); // scanner reads from standard input.

System.out.println("--------->Enter a directory name: ");
directoryName = scanner.nextLine().trim();
directory = new File(directoryName);

if (directory.isDirectory() == false) {
if (directory.exists() == false)
System.out.println("There is no such directory!");
else
System.out.println("That file is not a directory.");
} else {
System.out.println("\n--------->The content of the directory:");
listDirectoriesRecursively(directory, ""); // List the directory recursively
}
}

/**
* If the content is a file, print it with indentation.
* Otherwise, traverse directories recursively
*
* @param directory
* @param indentation
*/
private static void listDirectoriesRecursively(File directory, String indentation) {
System.out.println(indentation + directory.getName() + ":"); // Print the directory name
indentation += " "; // Increase the indentation for listing the contents.

String[] content = directory.list(); // Get the content of the directory

//If the content is a file, print it with indentation. Otherwise, traverse directories recursively
for (String name : content) {
File file = new File(directory, name);
if (file.isDirectory())
listDirectoriesRecursively(file, indentation); // traverse directories recursively
else
System.out.println(indentation + name);
}
}
}

2. Outputs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
--------->Enter a directory name: 
F:\3.UoPeople\2.My Learning Guide\6. CS11103 Programming 2\Course Materials

--------->The content of the directory:
Course Materials:
Week 1:
1. U1 L1 - Using NetBeans to Set Up A Simple Java Project.mp4
1. U1 L2 - Introduction to Jave and the Java Virtual Machine .mp4
1. U1 L3 - Variables, Datatypes, Expressions and Evaluation.mp4
Week 2:
JavaFX - VM.txt
Week 3:
Week 4:
BuggySearchAndSort.java
Code Unit 4-20220220.zip
GeneticProgramming_generations.txt
GeneticProgram_vs_RandomSearch.txt
Week 5:
Week 6:
Code Unit 6-20220306:
DirectoryList.java
DrawTextItem.java
DrawTextPanel.java
SimpleFileChooser.java
TextCollage.java

Process finished with exit code 0

​


This is the ending, thanks for reading.