标签:
public class FileUtil {
private FileUtil() {
}
public static void copyFile(String srcFile,String targetPath){
File src = new File(srcFile);
if(!src.exists()){
System.out.println("文件【"+src+"】不存在");
return;
}
System.out.println("正在复制文件:"+src);
int byteRead = 0;
try {
InputStream in = new FileInputStream(src);
File tarPath = new File(targetPath);
if(!tarPath.exists()){//如果目标路径不存在 , 则创建一个目标路径
tarPath.mkdir();
}
File tar = new File(tarPath.getAbsolutePath(),src.getName());
FileOutputStream out = new FileOutputStream(tar);
byte[] b = new byte[1024];//缓存
while((byteRead = in.read(b))!=-1){
out.write(b,0,byteRead);
}
in.close();
out.close();
System.out.println("文件复制完成!");
} catch (Exception e) {
System.out.println("文件复制失败!");
e.printStackTrace();
}
}
}
标签:
原文地址:http://www.cnblogs.com/nickhan/p/4226789.html