0

I'm trying to copy files using java. I have an arraylist of File objects that need copying but when the actual copy takes place the destination folder gets turned into a file and nothing is copied

                System.out.println("Dest: " + destPath.toString());

                ArrayList<File> fileList = listFiles(sourceDir);
                for (File file : fileList) {
                    Path sourcePath = Paths.get(file.getPath());
                    System.out.print("\r\nSource: " + sourcePath.toString());
                    CopyOption[] options = new CopyOption[] {
                            StandardCopyOption.REPLACE_EXISTING,
                            StandardCopyOption.COPY_ATTRIBUTES
                    };
                    try {
                        Files.copy(sourcePath, destPath, options);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }

The printed paths are: Dest: C:\Users\Ceri\Desktop\New folder (2)

Source: C:\Users\Ceri\Desktop\New folder\Blue cave floor.png Source: C:\Users\Ceri\Desktop\New folder\New Text Document.txt

Basically when i'm doing is trying to get a list of all changed/new files in a directory - specified by a text field - and copy them to another directory - again specified by a text field

the listFiles method returns the files

Ceri Turner
  • 830
  • 2
  • 12
  • 36
  • Note that, when already using the new I/O library **NIO**, there is no need to use `File` anymore. You could potentially rewrite `listFiles` to return `Path` objects instead using the `Files#walk` method: [How to read all files in a folder from Java?](https://stackoverflow.com/a/1846349/2411243) – Zabuzard Oct 20 '17 at 12:52
  • Also note that you should create your paths dynamically with a call like this: `Paths.get(System.getProperty("user.home"), "Desktop", "New folder", "New Text Document.txt")`. This approach is far more flexible and works **platform independent**. – Zabuzard Oct 20 '17 at 13:01

4 Answers4

1

The destination path needs to describe a file, if you wan't to copy a file.

Just add the filename to destPath.

Files.copy(sourcePath, destPath+"/"+file.getName(), options);

Herr Derb
  • 4,977
  • 5
  • 34
  • 62
  • That's working fine for the initial directory, but any other directories get created but no file is being copied – Ceri Turner Oct 20 '17 at 16:48
1

Source: C:\Users\Ceri\Desktop\New folder\Blue cave floor.png Source: C:\Users\Ceri\Desktop\New folder\New Text Document.txt

Make sure your slashes are correct first

  • If you are using backward slash, use \\
  • If you are using forward slash, use /

For example, change your paths to:

C:/Users/Ceri/Desktop/New folder/Blue cave floor.png 

Or

C:\\Users\\Ceri\\Desktop\\New folder\\Blue cave floor.png 

and try again.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • Best would be to **not even use slashes at all**. Usage of `Paths#get` for each folder would be far more flexible. Like `Paths.get("C://", "Users", "Ceri", "Desktop", "New Folder", "Blue cave floor.png")`. Note that `C:/Users/Desktop` should be accessed by its system value: `Paths.get(System.getProperty("user.home"), "Desktop")`. – Zabuzard Oct 20 '17 at 12:55
0

one approach is to use Apache commons IO FilesUtils.

    try {
        Path fileToCopy = Paths.get("path-of-file-to-copy");
        FileUtils.copyFile(fileToCopy.toFile(), new File("your-destination-path"));
    } catch (IOException e) {
        //handle
    }

or the other approach is to use standard Java NIO Files.copy() method

 try {
      Path fileToCopy = Paths.get("path-of-file-to-copy");
      Files.copy(fileToCopy, Paths.get("your-destination-path"));
    } catch (IOException e) {
       //handle
    }
LittleBigUllah
  • 313
  • 1
  • 3
  • 9
-1

If you are using Apache Commons - there is a FileUtils class you could use to copy the whole directory

try {
    FileUtils.copyDirectory(sourceDir, destPath);
} catch (IOException e) {
    e.printStackTrace();
}
achAmháin
  • 4,176
  • 4
  • 17
  • 40