Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Set;
- import java.util.TreeSet;
- import java.util.stream.Collectors;
- public class CheckFile {
- public static void main(String[] args) {
- hasTwoArgs(args);
- Path targetFolder = Paths.get(args[0]);
- isDirectory(targetFolder);
- Set<Path> targetChildren = children(targetFolder);
- Path fileList = Paths.get(args[1]);
- isReadable(fileList);
- Set<Path> listed = read(fileList);
- format(targetChildren, listed);
- }
- private static Set<Path> children(Path target) {
- Set<Path> ret = new TreeSet<>();
- Path current = Paths.get(".").toAbsolutePath();
- try {
- for (Path p: Files.list(target).collect(Collectors.toList())) {
- if (Files.isRegularFile(p)) {
- ret.add(current.relativize(p.toAbsolutePath()).toAbsolutePath());
- continue;
- }
- }
- } catch (Exception e) {
- e.printStackTrace(System.err);
- System.exit(1);
- }
- return ret;
- }
- private static void format(Set<Path> target, Set<Path> list) {
- Path current = Paths.get(".").toAbsolutePath();
- for (Path p: target) {
- if (list.contains(p)) {
- System.out.print("○ ");
- } else {
- System.out.print("× ");
- }
- System.out.println(current.relativize(p.toAbsolutePath()));
- }
- }
- private static void hasTwoArgs(String[] args) {
- if (args.length != 2) {
- usage();
- }
- }
- private static void isDirectory(Path target) {
- if (Files.isDirectory(target)) {
- return;
- }
- usage();
- }
- private static void isReadable(Path path) {
- if (Files.isReadable(path)) {
- return;
- }
- usage();
- }
- private static Set<Path> read(Path path) {
- Set<Path> ret = new TreeSet<>();
- try {
- for (String s: Files.readAllLines(path)) {
- ret.add(Paths.get(s).toAbsolutePath());
- }
- } catch (Exception e) {
- e.printStackTrace(System.err);
- System.exit(1);
- }
- return ret;
- }
- private static void usage() {
- System.err.println("ERROR: usage: java " + CheckFile.class.getName() + " folder filelist");
- System.exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement