1.相同系统复制
/**
* 复制文件
* @param path
* @param name
* @param file
* @return
*/
protected boolean moveFile(String path, String name, File file){
File direct = new File(path);
if(!direct.exists()) direct.mkdirs();
File newFile = new File(path+File.separator+name);
file.renameTo(newFile);
file.delete();
return true;
}
2.通用的复制文件
/**
* copy file
*
* @param orig
* @param dest
*/
public static void copyFile(File orig, File dest){
FileOutputStream fos = null;
FileChannel foc = null;
FileInputStream fis = null;
FileChannel fic = null;
try{
fos = new FileOutputStream(dest);
fis = new FileInputStream(orig);
fic = fis.getChannel();
foc = fos.getChannel();
fic.transferTo(0, fic.size(), foc);
}catch(Exception e){
log.error("复制文件失败", e);
}finally{
if(foc!=null){
try{
foc.close();
}catch(Exception e){
log.error("关闭通道异常", e);
}
}
if(fos!=null){
try{
fos.close();
}catch (Exception e) {
log.error("关闭文件流失败", e);
}
}
if(fic!=null){
try{
fic.close();
}catch(Exception e){
log.error("关闭通道异常", e);
}
}
if(fis!=null){
try{
fis.close();
}catch (Exception e) {
log.error("关闭文件流失败", e);
}
}
}
}