TizzyT

File/Directory Duplicator -TizzyT

Feb 16th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. package duplicatedir;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.RandomAccessFile;
  6. import java.nio.channels.FileChannel;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9.  
  10. public class DuplicateDir {
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner kb = new Scanner(System.in);
  14.         System.out.println("Source Path:");
  15.         String src = kb.nextLine();
  16.         System.out.println("================================================");
  17.         System.out.println("Destination Path:");
  18.         try {
  19.             String des = kb.nextLine();
  20.             System.out.println("================================================");
  21.             File Dir1 = new File(src);
  22.             File Dir2 = new File(des);
  23.             if (Dir1.equals(Dir2)) {
  24.                 System.out.println("Error: Source and Destination Match");
  25.             } else {
  26.                 CrawlerCopy(Dir1, Dir2);
  27.                 System.out.println("COMPLETED");
  28.             }
  29.         } catch (Exception ex) {
  30.             System.out.println(ex.getMessage());
  31.         }
  32.     }
  33.  
  34.     private static void CrawlerCopy(File source, File destination) throws Exception {
  35.         if (source.exists()) {
  36.             if (source.isDirectory()) {
  37.                 if (!destination.exists()) {
  38.                     System.out.println("Making Dir:");
  39.                     System.out.println("      Path: " + destination.getAbsolutePath());
  40.                     destination.mkdir();
  41.                     System.out.println("DONE");
  42.                     System.out.println("================================================");
  43.                 }
  44.                 ArrayList<File> folders = new ArrayList<>();
  45.                 String[] stuff = source.list();
  46.                 for (String s : stuff) {
  47.                     File crnt = new File(source.getAbsolutePath() + "\\" + s);
  48.                     if (crnt.isFile()) {
  49.                         copyFile(crnt, new File(destination.getAbsolutePath() + "\\" + crnt.getName()));
  50.                     }
  51.                     if (crnt.isDirectory()) {
  52.                         folders.add(crnt);
  53.                     }
  54.                 }
  55.                 for (File f : folders) {
  56.                     CrawlerCopy(f, new File(destination.getAbsolutePath() + "\\" + f.getName()));
  57.                 }
  58.             } else if (source.isFile()) {
  59.                 copyFile(source, destination);
  60.             }
  61.         }
  62.     }
  63.  
  64.     public static void copyFile(File sourceFile, File destFile) throws IOException {
  65.         if (!destFile.exists()) {
  66.             destFile.createNewFile();
  67.         }
  68.         FileChannel source = null;
  69.         FileChannel destination = null;
  70.         try {
  71.             source = new RandomAccessFile(sourceFile, "rw").getChannel();
  72.             destination = new RandomAccessFile(destFile, "rw").getChannel();
  73.             long position = 0;
  74.             long count = source.size();
  75.             System.out.println("Copying:");
  76.             System.out.println("   From: " + sourceFile.getAbsolutePath());
  77.             System.out.println("     To: " + destFile.getAbsolutePath());
  78.             source.transferTo(position, count, destination);
  79.             System.out.println("DONE");
  80.         } catch (IOException e) {
  81.             System.out.println("FAILED");
  82.         } finally {
  83.             if (source != null) {
  84.                 source.close();
  85.             }
  86.             if (destination != null) {
  87.                 destination.close();
  88.             }
  89.             System.out.println("================================================");
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment