移動檔案

/**

* 移動檔案

*

* @param sourcefile File 檔案來源

* @param newpath String 目的地路徑

* @return

*/

public boolean moving(File sourcefile, String newpath) {

File folder = new File(newpath);

//目的地資料夾是否存在,不存在建立

if (!folder.isDirectory()) {

folder.mkdirs();

}

File f1 = new File(newpath + System.getProperty("file.separator") + sourcefile.getName());

//檔案是否存在,存在刪除

if (f1.exists()) {

f1.delete();

}

//檔案搬移

boolean result = sourcefile.renameTo(f1);

if (result) {

System.out.println("由" + sourcefile.getPath() + "搬移" + sourcefile.getName() + "檔案至" + f1.getPath() + "成功。");

} else {

System.out.println("由" + sourcefile.getPath() + "搬移" + sourcefile.getName() + "檔案至" + f1.getPath() + "失敗。");

}

return result;

}