After running this site for a little more than three years, it’s rare that I’ll come across a snippet of code that leaves me speechless. Today’s snippet comes from a large Java application that Byron recently started working on and is … well, I’ll just let the code do the talking…
public void copyFile(File sourceFile, File destFile) {
File batchFile = createBatchFile();
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(batchFile.getAbsolutePath()
+ " " + sourceFile.getAbsolutePath()
+ " " + destFile.getAbsolutePath());
p.waitFor();
if (sourceFile.exists()){
sourceFile.delete();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public File createBatchFile(){
File batchFile = new File("C:\\copyFile.bat");
if (!batchFile.exists()){
utils.FileIO.writeFile(
batchFile.getAbsolutePath(),
"cd\\ \n");
utils.FileIO.appendFile(
batchFile.getAbsolutePath(),
"copy %1 %2 \n");
}
return batchFile;
}