思路:
- 字节输入流 读入内存
- 字节输出流 写出
注意: 边读边写 最后一次读取 很可能没有1024字节 ;所以需要count辅助记录 每次只写count 保证正确
//设计一个静态方法
// 实现给顶任意的路径文件 可以复制到当前d盘下
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class copy_file {
public static void copyFile(String fileName) throws IOException {
//本质就是 读入 又写出 所以
//创建 字节流
FileInputStream fileInputStream = new FileInputStream(fileName);
FileOutputStream fileOutputStream = new FileOutputStream("d:\\中心2.xlsx");
//读文件 到字节数组 数组写入文件
byte[] buff = new byte[1024];
int count = 0;
while ((count = fileInputStream.read(buff)) != -1) {
fileOutputStream.write(buff, 0, count);//注意 最后一部分 可能没有1024 所有只写 读入的那部分
}
fileInputStream.close();
fileOutputStream.close();
System.out.println("复制完毕!");
}
public static void main(String[] args) throws IOException {
copyFile("D:\\中心.xlsx");
System.out.println("文件复制完毕!");
}
}