-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.java
More file actions
64 lines (48 loc) · 2.02 KB
/
ls.java
File metadata and controls
64 lines (48 loc) · 2.02 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.OutputStreamWriter;
/*
|-----------------------------------|
|------------Basic Shell------------|
|--------------ls.java--------------|
|-----------------------------------|
|>> Role : Lists all files and |
|directories in the folder where |
|this program is currently located |
|>> Shell Command : 'ls'. |
|>> Arguments : None. |
|>> Pipe Support : Output Only |
\-----------------------------------/
*/
class ls {
// Main Function
public static void main(String args[]) {
// Argument passed by the shell is either 'nopipe' or 'pipeout'
// If 'nopipe' : List of files and directories will be displayed by the shell
// If 'pipeout' : List of files and directories will be passed as arguments to another command using pipes & stdout
if (args[0].equals("nopipe")) {
//Fetches the names of all files and directories in the folder where the program is located, and prints them to shell
String[] pathnames;
File f = new File(ls.class.getProtectionDomain().getCodeSource().getLocation().getPath());
pathnames = f.list();
for (String pathname: pathnames) {
System.out.println(pathname);
}
} else if (args[0].equals("pipeout")) {
//Fetches the names of all files and directories in the folder where the program is located, and passes then to stdout
String[] pathnames;
File f = new File(ls.class.getProtectionDomain().getCodeSource().getLocation().getPath());
pathnames = f.list();
try {
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
for (String pathname: pathnames) {
log.write(pathname);
}
log.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}