we are in complete agreement with the replies. We just meant to say that while renameTo() is the default option available, make sure to verify that it succeeds.
The below code can be a platform independent option to perform a copy operation.
import java.io.*;
boolean fileCopied = false;
InputStream in = null;
OutputStream out = null;
String sepr = System.getProperty("file.separator");
try {
in = new FileInputStream(path+sepr+srcFileName);
File newFile = new File(path+sepr+targetFileName);
out = new FileOutputStream(path+sepr+targetFileName);
byte[] buffer = new byte[2048];
while (true) {
synchronized (buffer) {
int length = in.read(buffer);
if (length != -1) {
out.write(buffer, 0, length);
}
else
break;
}
}
File oldFile = new File(path+sepr+srcFileName);
fileCopied = newFile.exists();
}
catch(Throwable t){
//handle exceptions here.
}
finally {
try{
if (in != null) { in.close();}
if (out != null) { out.close(); }
}catch(Throwable t1){}
}
return fileCopied;
Still, note that this is the code to do a copy operation. (It doen't move any file)
Hope this is more clear now.
Sincerly,
Your friends at
www.javaadvice.com www.javaadvice.com - The place where your questions are answered directly.