Skip to content

Commit b0757de

Browse files
Implement LsCommand to list directory contents
1 parent 4c6758f commit b0757de

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.io.File;
2+
3+
public class LsCommand {
4+
5+
public static void main(String[] args) {
6+
// Determine the directory to list
7+
String directoryPath = "."; // current directory by default
8+
if (args.length > 0) {
9+
directoryPath = args[0]; // if user provides a path
10+
}
11+
12+
File directory = new File(directoryPath);
13+
14+
if (!directory.exists()) {
15+
System.out.println("Directory does not exist: " + directoryPath);
16+
return;
17+
}
18+
19+
if (!directory.isDirectory()) {
20+
System.out.println(directoryPath + " is not a directory.");
21+
return;
22+
}
23+
24+
// List files and directories
25+
String[] contents = directory.list();
26+
if (contents != null && contents.length > 0) {
27+
for (String item : contents) {
28+
System.out.println(item);
29+
}
30+
} else {
31+
System.out.println("Directory is empty.");
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)