aboutsummaryrefslogtreecommitdiff
path: root/Week12 Find File/src/com/camilstaps/findfile/FileFinder.java
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/com/camilstaps/findfile/FileFinder.java
parenttarball week11 (diff)
Week12 Find file
Diffstat (limited to 'Week12 Find File/src/com/camilstaps/findfile/FileFinder.java')
-rw-r--r--Week12 Find File/src/com/camilstaps/findfile/FileFinder.java51
1 files changed, 51 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) {}
+ }
+ }
+ }
+
+}