0

I'm trying to transfer over files from one directory to another. I am currently using this method to accomplish this task:

File srcDir = new File("path\to\file");
File destDIr = new File("path\to\file");
File.Utils.copyDirectory(srcDir, destDir);

However what I want is to not overwrite files that already exist in the destDir with the files in the srcDir.

For example if I have a file struct like:

├───srcDir
│   ├───this.txt
│   ├───hello.txt
│   ├───main.java

├───destDir
│   ├───this.txt

I want to copy over hello.txt and main.java however I do not want to copy/update/replace this.txt

I was thinking I could just check if file exists with something like

if(f.exists() && !f.isDirectory())

but that seems a little bit hacky and doesn't actually work the way I intend.

I'm looking for a common, simple way to do this, thank you.

Is there a solution that could also work with sub directories as well?

  • Your code will be vastly more robust if you use the [java.nio.file](http://docs.oracle.com/javase/8/docs/api/java/nio/file/package-summary.html) package instead of the 20-year-old java.io.File class. And you'll find the `Files` class has a number of "walk" methods which traverse a tree recursively. – VGR Dec 18 '15 at 19:24
  • maybe try to prepare two Sets of names and perform a logical complement, sth like diff = a.clone(); diff.removeAll(b); – Sebastian Kramer Dec 18 '15 at 19:31
  • @VGR I was trying that before but I receive a filealreadyexistsexception –  Dec 18 '15 at 19:35
  • What's wrong with checking whether a file exists before overwriting it? That seems like the most concise possible approach. – VGR Dec 18 '15 at 20:10
  • @VGR I'm not sure how I refer to a file when using the `Files.copy()` function to see if it exists.. I sorta figured there was an existing function in nio that could copy a directory over without replacement –  Dec 18 '15 at 20:12
  • I think I would just call [Files.exists](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#exists-java.nio.file.Path-java.nio.file.LinkOption...-) before copying. – VGR Dec 18 '15 at 20:13
  • @VGR I'm not sure how to specify the path though for an arbitrary file. with the copy method I just paass in the path to a directory and it does it's thing –  Dec 18 '15 at 20:16
  • I'm not sure I understand. Any Path you can pass to Files.copy, you can pass to Files.exists. – VGR Dec 18 '15 at 21:07

1 Answers1

-1

You can try with this code. It works for me.

public class CopyDirectoryWithoutOverWritting 
{
    public static void main(String[] args)
    {   
        File srcFolder = new File("C:/Users/Usuarioç/Desktop/FACHA");
    File destFolder = new File("C:/Users/Usuarioç/Desktop/OTRO");
    //make sure source exists
    if(!srcFolder.exists()){

       System.out.println("Directory does not exist.");
       //just exit
       System.exit(0);

    }else{

       try{
        copyFolder(srcFolder,destFolder);
       }catch(IOException e){
        e.printStackTrace();
        //error, just exit
         System.exit(0);
       }
    }

    System.out.println("Done");
}

public static void copyFolder(File src, File dest)
    throws IOException{

    if(src.isDirectory()){

        //if directory not exists, create it
        if(!dest.exists()){
           dest.mkdir();
           System.out.println("Directory copied from " 
                          + src + "  to " + dest);
        }

        //list all the directory contents
        String files[] = src.list();

        for (String file : files) {
           //construct the src and dest file structure
           File srcFile = new File(src, file);
           File destFile = new File(dest, file);
           if(!destFile.isDirectory() && !destFile.exists()){
               //recursive copy
               copyFolder(srcFile,destFile);
           }
        }

    }else{
        //if file, then copy it
        //Use bytes stream to support all file types
        InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest); 

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes 
            while ((length = in.read(buffer)) > 0){
               out.write(buffer, 0, length);
            }

            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);
    }
}
}