Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class BackupJob implements Runnable {
  4.  
  5.     private File startDir;
  6.     private File destDir;
  7.     private Filter filter;
  8.    
  9.     public BackupJob(File s, File d){
  10.         this(s, d, null);
  11.     }
  12.    
  13.     public BackupJob(File s, File d, Filter f){
  14.         startDir = s;
  15.         destDir = d;
  16.         filter = f;
  17.     }
  18.    
  19.     @Override
  20.     public void run() {
  21.         try {
  22.             validateDirectory(startDir);
  23.             validateDirectory(destDir);
  24.            
  25.             File[] contents = startDir.listFiles();
  26.            
  27.             for (int i = 0; i < contents.length; i++){
  28.                 recurse(contents[i]);
  29.             }
  30.         } catch (FileNotFoundException e) {
  31.             // TODO Auto-generated catch block
  32.             e.printStackTrace();
  33.         }  
  34.     }
  35.    
  36.     private void recurse(File file){
  37.         if (!filter.isAcceptable(file))
  38.             return;
  39.        
  40.         File dest = new File (destDir, getRelPath(file, startDir));
  41.        
  42.         if (file.isDirectory()){
  43.             if(!dest.exists()){
  44.                 dest.mkdirs();
  45.                 File [] contents = file.listFiles();
  46.                
  47.                 for (int i = 0; i < contents.length; i++){
  48.                     recurse(contents[i]);
  49.                 }
  50.             }
  51.         } else {
  52.             if(dest.exists() && dest.lastModified() >= file.lastModified()
  53.                     && dest.length() == file.length()){
  54.                 System.out.println("File exists, not copied.");
  55.             } else {
  56.                 dest.getParentFile().mkdirs();
  57.                 CopyJob cj = new CopyJob(file, dest);
  58.                 //TODO: get job to pool
  59.             }
  60.         }
  61.     }
  62.  
  63.     //returns the path of f relative to the given root directory
  64.     private String getRelPath (File f, File root){
  65.         String p = f.getAbsolutePath();
  66.         return p.replace(f.getParent(), "");
  67.     }
  68.    
  69.     /**
  70.      * Directory is valid if it exists, does not represent a file, and can be read.
  71.      */
  72.      private void validateDirectory (File aDirectory) throws FileNotFoundException {
  73.        if (aDirectory == null) {
  74.          throw new IllegalArgumentException("Directory should not be null.");
  75.        }
  76.        if (!aDirectory.exists()) {
  77.          throw new FileNotFoundException("Directory does not exist: " + aDirectory);
  78.        }
  79.        if (!aDirectory.isDirectory()) {
  80.          throw new IllegalArgumentException("Is not a directory: " + aDirectory);
  81.        }
  82.        if (!aDirectory.canRead()) {
  83.          throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
  84.        }
  85.      }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement