Skip to content

Commit

Permalink
Add safety check for files before running
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRagstad committed Aug 5, 2021
1 parent 5f40470 commit 2971ac5
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Runner.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -33,19 +34,31 @@ public static void run(String[] files) {
return;
}
for (String file : files) {
File f = new File(file);
if (!f.exists()) {
System.out.println("File '" + file + "' does not exist.");
return;
} else if (f.isDirectory()) {
System.out.println("File '" + file + "' is a directory.");
return;
} else if (!f.canRead()) {
System.out.println("File '" + file + "' is not readable.");
return;
}
String source;
try {
source = Files.readString(Paths.get(file), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
continue;
}
i.clean(); // New environment for each file.
try {
i.evalAll(source); // Discard last the expressions value
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
i.clean(); // New environment for each file.
}
}
}

0 comments on commit 2971ac5

Please sign in to comment.