首页 > 代码库 > Java复制文件
Java复制文件
public class FileUtil {
private FileUtil() {
}
public static void copyFile(String srcFile,String targetPath){
File src = http://www.mamicode.com/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();
}
}
}
Java复制文件