diff options
Diffstat (limited to 'Week12 Find File/src/com/camilstaps/findfile/FileFinder.java')
-rw-r--r-- | Week12 Find File/src/com/camilstaps/findfile/FileFinder.java | 51 |
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) {} + } + } + } + +} |