Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package part1;
- import java.io.File;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author matt
- */
- public class DirectoryHasher implements Runnable {
- private File inDir;
- private File outDir;
- public DirectoryHasher(File inDir, File outDir) {
- this.inDir = inDir;
- this.outDir = outDir;
- }
- public DirectoryHasher(String inDir, String outDir) {
- this(new File(inDir), new File(outDir));
- }
- @Override
- public void run() {
- try {
- // create output directory if doesn't already exist
- if (!outDir.exists()) {
- if (!outDir.mkdirs() && !outDir.exists()) {
- throw new IOException("Couldn't create directory `" + outDir.getAbsolutePath() + "`");
- }
- }
- // first, we need to iterate through the files in the current directory
- File[] files = inDir.listFiles();
- if (files != null) {
- for (File f : files) {
- if (f.isFile()) {
- FileHasher fh = new FileHasher(f, outDir);
- new Thread(fh).start();
- } else if (f.isDirectory()) {
- // recursively call this method
- File nextOutDir = new File(outDir.getAbsolutePath() +
- File.separator + f.getName());
- DirectoryHasher dh = new DirectoryHasher(f, nextOutDir);
- new Thread(dh).start();
- }
- }
- }
- } catch (IOException ex) {
- Logger.getLogger(DirectoryChecker.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Add Comment
Please, Sign In to add comment