aboutsummaryrefslogtreecommitdiff
path: root/Week12 Find File/src
diff options
context:
space:
mode:
authorCamil Staps2015-05-12 13:17:53 +0200
committerCamil Staps2015-05-12 13:17:53 +0200
commit81b558651a3609dd8ac1bc731ae629f85178bab7 (patch)
treea22772e6a1692399fab84b03bc0baf0085e96550 /Week12 Find File/src
parenttarball week11 (diff)
Week12 Find file
Diffstat (limited to 'Week12 Find File/src')
-rw-r--r--Week12 Find File/src/com/camilstaps/findfile/FileFinder.java51
-rw-r--r--Week12 Find File/src/com/camilstaps/findfile/Week12FindFile.java31
2 files changed, 82 insertions, 0 deletions
diff --git a/Week12 Find File/src/com/camilstaps/findfile/FileFinder.java b/Week12 Find File/src/com/camilstaps/findfile/FileFinder.java
new file mode 100644
index 0000000..80e06da
--- /dev/null
+++ b/Week12 Find File/src/com/camilstaps/findfile/FileFinder.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2015 Camil Staps
+ */
+package com.camilstaps.findfile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author camilstaps
+ */
+public class FileFinder implements Runnable {
+
+ private final File rootDir;
+ private final String search;
+
+ public FileFinder(String root, String search) throws IOException {
+ rootDir = new File(root);
+ if (!rootDir.exists() || !rootDir.isDirectory()) {
+ throw new IOException(root + " is not a directory.");
+ }
+
+ this.search = search;
+ }
+
+ @Override
+ public void run() {
+ find(rootDir, search);
+ }
+
+ private void find(File dir, String name) {
+ File[] files = rootDir.listFiles();
+ if (files == null) return;
+ for (File file : files) {
+ if (file.getName().equals(name)) {
+ System.out.println(file.getAbsolutePath());
+ }
+ if (file.isDirectory()) {
+ try {
+ Thread t = new Thread(new FileFinder(file.getAbsolutePath(), search));
+ t.start();
+ t.join();
+ } catch (IOException | InterruptedException ex) {}
+ }
+ }
+ }
+
+}
diff --git a/Week12 Find File/src/com/camilstaps/findfile/Week12FindFile.java b/Week12 Find File/src/com/camilstaps/findfile/Week12FindFile.java
new file mode 100644
index 0000000..4c59e7e
--- /dev/null
+++ b/Week12 Find File/src/com/camilstaps/findfile/Week12FindFile.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2015 Camil Staps
+ */
+package com.camilstaps.findfile;
+
+import java.io.IOException;
+
+/**
+ *
+ * @author camilstaps
+ */
+public class Week12FindFile {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ if (args.length != 2) {
+ System.err.println("Usage: java Week12FindFile <searchdir> <filename>");
+ System.exit(-1);
+ }
+
+ try {
+ Thread t = new Thread(new FileFinder(args[0], args[1]));
+ t.start();
+ } catch (IOException ex) {
+ System.err.println(ex.toString());
+ }
+ }
+
+}