複製檔案

/**

* 複製檔案

*

* @param sourcefile 資料來源

* @param temppath 複製的目的地

*/

public void copyfile(File sourcefile, File temppath) 一{

try {

String[] FileList = sourcefile.list();

for (String ff : FileList) {

File file = new File(sourcefile.getAbsolutePath() + System.getProperty("file.separator") + ff);

File tempfile = new File(temppath.getAbsoluteFile() + System.getProperty("file.separator") + ff);

//目的地資料夾是否存在,如果不存在則新建

if(!temppath.isDirectory()){

temppath.mkdirs();

}

//建立目的地檔案

tempfile.createNewFile();

InputStream in = new FileInputStream(file);

OutputStream out = new FileOutputStream(tempfile);

byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) > 0) {

out.write(buf, 0, len);

}

in.close();

out.close();

}

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

} catch (FileNotFoundException ex) {

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

} catch (IOException e) {

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

}

}