Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java.nio.files - Copying files

Can anyone tell me what I've done wrong with the following code. I receive no errors - it just goes straight to the catch.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

    public static void main(String[] args) {
         Path source = Paths.get("C:\\Users\\Public\\Pictures\\SamplePictures");
    Path nwdir = Paths.get("D:\\NetbeansProjects\\CopyingFiles\\copiedImages");

    try{
    Files.copy(source, nwdir);
    }catch (IOException e){
        System.out.println("Unsucessful. What a surprise!");
    }
    }
}
like image 629
user1311422 Avatar asked Apr 12 '12 15:04

user1311422


1 Answers

If you take a look at the Javadocs of Files.copy, you'll notice this line (emphasis added):

If the file is a directory then it creates an empty directory in the target location (entries in the directory are not copied). This method can be used with the walkFileTree method to copy a directory and all entries in the directory, or an entire file-tree where required.

So it looks like you need to use that walkFileTree method.

(And as the commenters said, print out exceptions and they'll often tell you what's wrong!)

like image 131
Jon7 Avatar answered Sep 19 '22 11:09

Jon7