Skip to content

Commit 906a0d6

Browse files
committed
add Java example
1 parent 1de645d commit 906a0d6

File tree

2 files changed

+103
-1
lines changed

2 files changed

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

0 commit comments

Comments
 (0)