0

Possible Duplicate:
Standard concise way to copy a file in Java?

I have a set of .png files in a folder at location C:\Desktop\input\Acsady\dZI_3.png. Now I wish to make a copy of the image into a different location C:\Desktop\input\final\Acsady\5_dZI_3.png. How do I go about doing it? I am currently having the following code which is not giving me the expected result.

String newName = outdir + File.separator + "final" + File.separator + nfname;
File newfname = new File(outdir + File.separator + "final" + File.separator + nfname);
String source = outdir + File.separator + lkupfname;
String target = outdir + File.separator + "final" + File.separator + nfname;

System.out.println("Copying file: "+source +" to "+target);

boolean status = filename.renameTo(newfname);
System.out.println("status : "+status);

I get status as false everytime which means that renameTo function was not successfull.

Community
  • 1
  • 1
Gopal Samant
  • 2,109
  • 3
  • 15
  • 11

2 Answers2

0

Take a look at Files.copy if you are on Java 1.7.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

This is extracted from java docs. May be helpful to you to find root cause.

Renames the file denoted by this abstract pathname. Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

someone
  • 6,577
  • 7
  • 37
  • 60