Alexis Zecharies
Alexis Zecharies

Reputation: 159

Copy one file from a folder to another folder in java

I am trying to copy a file form a folder to another folder

i have tried what was suggested in other posts but i have not been successful

Copying files from one directory to another in Java

this has not worked for me

the file is C:/Users/win7/Desktop/G1_S215075820014_T111_N20738-A_D2015-01-26_P_H0.xml

the destination folder is C:/Users/win7/Desktop/destiny

this is the copy code

String origen = "C:/Users/win7/Desktop/G1_S215075820014"
               +"_T111_N20738-A_D2015-01-26_P_H0.xml";

String destino = "C:/Users/win7/Desktop/destiny";

private void copiarArchivoACarpeta(String origen, String destino) throws IOException {
    Path FROM = Paths.get(origen);
    Path TO = Paths.get(destino);
    CopyOption[] options =
            new CopyOption[] {StandardCopyOption.REPLACE_EXISTING,
                StandardCopyOption.COPY_ATTRIBUTES };
    java.nio.file.Files.copy(FROM, TO, options);
}

Upvotes: 0

Views: 942

Answers (1)

Grim
Grim

Reputation: 2040

Try:

java.nio.file.Files.copy(FROM, TO.resolve(FROM.getFileName()),
    StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);

Because the second parameter must be a Path to a file that not yet exists.

Just like the docu sais: Documentation

Upvotes: 1

Related Questions