结合FileOutputStream和FileInputStream实现简单文件复制
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
public class FileCopyDemo {
public boolean fileCopy(String src, String desc) {
File srcFile, descFile;
srcFile = new File(src);
descFile = new File(desc);
// 准备流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 生成文件备用
descFile.createNewFile();
// 实例化流对象
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(descFile);
int buffRead;
// 字节数组,用于缓存
byte[] buff = new byte[4*1024];
// 读取文件内容到缓存
while((buffRead = fis.read(buff)) != -1) {
// 数据写入文件
fos.write(buff, 0, buffRead);
}
// 清空缓存
fos.flush();
// 关闭流
fos.close();
fis.close();
} catch (IOException e) {
System.out.println(e);
// 正常返回
return false;
}
// 异常返回
return true;
}
public static void main(String[] args) {
// 实例化对象
FileCopyDemo fcd = new FileCopyDemo();
String args[0];
String desc = args[1];
// 进行文件复制
if(fcd.fileCopy(src, desc)) {
System.out.println("文件复制成功!");
} else {
System.out.println("文件复制失败!");
}
}
}输出



















