0

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);
}
Community
  • 1
  • 1
  • 1
    We can´t help you without knowing neither the error you are getting nor seeing any code. – SomeJavaGuy Feb 25 '15 at 14:28
  • 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); } – Alexis Zecharies Feb 25 '15 at 14:32
  • I think the problem is you are trying to use `Files.copy` from a file to a folder destination instead of a file destination. – gtgaxiola Feb 25 '15 at 14:35
  • but that works to copy a file to another file not to a folder! – Alexis Zecharies Feb 25 '15 at 14:38
  • possible duplicate of [Standard concise way to copy a file in Java?](http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java) – SeniorJD Feb 25 '15 at 14:40
  • @AlexisZecharies Well it seems to work.. Are you sure you are not seeing a `destiny` file with no extensions under your `C:/Users/win7/Desktop/` path? – gtgaxiola Feb 25 '15 at 14:40
  • gtgaxiola that is what happens but what i want is to copy the file into the folder, which is not happening – Alexis Zecharies Feb 25 '15 at 14:43

1 Answers1

1

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

Grim
  • 1,938
  • 10
  • 56
  • 123