Skip to content

Commit ea4c815

Browse files
committed
add Java example
1 parent 1de645d commit ea4c815

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
Filesystem.class
12
code-coverage/
23
test-results/
34
*.asv
4-
docs/
5+
docs/

example/Filesystem.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Filesystem class usable from the command line
2+
//
3+
// useful to test Java functions before / while implementing in Matlab
4+
//
5+
// usage:
6+
// $ javac Filesystem.java
7+
// $ java -cp . Filesystem
8+
//
9+
// To install Java Development Kit (JDK): https://www.scivision.dev/install-jdk/
10+
11+
import java.util.Scanner;
12+
import java.util.ArrayList;
13+
import java.io.File;
14+
15+
16+
public class Filesystem {
17+
public static void main(String[] args) {
18+
Scanner scanner = new Scanner(System.in);
19+
20+
String argument = "";
21+
final String prompt = "Jfs> ";
22+
23+
System.out.print(prompt);
24+
25+
while (scanner.hasNextLine()) {
26+
String input = scanner.nextLine();
27+
28+
String[] inputArray = input.split(" ");
29+
String command = inputArray[0];
30+
31+
if (inputArray.length > 1) {
32+
argument = inputArray[1];
33+
} else {
34+
argument = "";
35+
}
36+
37+
if (command.equals("exit") || command.equals("q") || command.equals("\u0004")) {
38+
break;
39+
} else if (command.equals("java_version")) {
40+
System.out.println(System.getProperty("java.version"));
41+
} else if (command.equals("absolute")) {
42+
System.out.println(absolute(argument));
43+
} else if (command.equals("expanduser")) {
44+
System.out.println(expanduser(argument));
45+
} else if (command.equals("canonical")) {
46+
System.out.println(canonical(argument));
47+
} else if (command.equals("is_absolute")) {
48+
System.out.println(is_absolute(argument));
49+
} else if (command.equals("is_exe")) {
50+
System.out.println(is_exe(argument));
51+
} else if (command.equals("is_readable")) {
52+
System.out.println(is_readable(argument));
53+
} else if (command.equals("ram_free")) {
54+
System.out.println(ram_free());
55+
} else {
56+
System.err.println("Command not found");
57+
}
58+
59+
System.out.print(prompt);
60+
}
61+
}
62+
63+
public static String absolute(String path) {
64+
File f = new File(path);
65+
return f.getAbsolutePath();
66+
}
67+
68+
public static String expanduser(String path) {
69+
if (path.startsWith("~"))
70+
return System.getProperty("user.home") + path.substring(1);
71+
72+
return path;
73+
}
74+
75+
public static String canonical(String path) {
76+
try {
77+
File p = new File(path);
78+
return p.getCanonicalPath();
79+
} catch (java.io.IOException e) {
80+
return path;
81+
}
82+
}
83+
84+
public static Boolean is_absolute(String path) {
85+
return new File(path).isAbsolute();
86+
}
87+
88+
public static Boolean is_exe(String path) {
89+
File f = new File(path);
90+
return f.isFile() && f.canExecute();
91+
}
92+
93+
public static Boolean is_readable(String path) {
94+
return new File(path).canRead();
95+
}
96+
97+
public static Long ram_free() {
98+
com.sun.management.OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean)
99+
java.lang.management.ManagementFactory.getOperatingSystemMXBean();
100+
return os.getFreeMemorySize();
101+
}
102+
}

0 commit comments

Comments
 (0)