forked from uclid/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryList.java
More file actions
42 lines (33 loc) · 1.37 KB
/
DirectoryList.java
File metadata and controls
42 lines (33 loc) · 1.37 KB
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
import java.io.File;
import java.util.Scanner;
/**
* This program lists the files in a directory specified by
* the user. 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.print("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 {
files = directory.list();
System.out.println("Files in directory \"" + directory + "\":");
for (int i = 0; i < files.length; i++)
System.out.println(" " + files[i]);
}
scanner.close();
} // end main()
} // end class DirectoryList