Guest User

Untitled

a guest
Jun 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package part1;
  6.  
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. /**
  13.  *
  14.  * @author matt
  15.  */
  16. public class DirectoryHasher implements Runnable {
  17.     private File inDir;
  18.     private File outDir;
  19.    
  20.     public DirectoryHasher(File inDir, File outDir) {
  21.         this.inDir = inDir;
  22.         this.outDir = outDir;
  23.     }
  24.    
  25.     public DirectoryHasher(String inDir, String outDir) {
  26.         this(new File(inDir), new File(outDir));
  27.     }
  28.  
  29.     @Override
  30.     public void run() {
  31.         try {
  32.             // create output directory if doesn't already exist
  33.             if (!outDir.exists()) {
  34.                 if (!outDir.mkdirs() && !outDir.exists()) {
  35.                     throw new IOException("Couldn't create directory `" + outDir.getAbsolutePath() + "`");
  36.                 }
  37.             }
  38.             // first, we need to iterate through the files in the current directory
  39.             File[] files = inDir.listFiles();
  40.             if (files != null) {
  41.                 for (File f : files) {
  42.                     if (f.isFile()) {
  43.                         FileHasher fh = new FileHasher(f, outDir);
  44.                         new Thread(fh).start();
  45.                     } else if (f.isDirectory()) {
  46.                         // recursively call this method
  47.                         File nextOutDir = new File(outDir.getAbsolutePath() +
  48.                                 File.separator + f.getName());
  49.                         DirectoryHasher dh = new DirectoryHasher(f, nextOutDir);
  50.                         new Thread(dh).start();
  51.                     }
  52.                 }
  53.             }
  54.         } catch (IOException ex) {
  55.             Logger.getLogger(DirectoryChecker.class.getName()).log(Level.SEVERE, null, ex);
  56.         }
  57.     }
  58.    
  59. }
Add Comment
Please, Sign In to add comment