package Day0213;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author CF
* @version 1.0
* @date 2023/2/13 12:34
*/
public class Test03 {
/*
需求 : 把 "图片路径\xxx.jpg" 复制到当前模块下
分析:
复制文件,其实就把文件的内容从一个文件中读取出来(数据源),然后写入到另一个文件中(目的地)
数据源:
xxx.jpg --- 读数据 --- FileInputStream
目的地:
模块名称\copy.jpg --- 写数据 --- FileOutputStream
*/
public static void main(String[] args) throws IOException {
// 创建字节输入流对象
FileInputStream fis = new FileInputStream("D:\\2022-12-01 112455.jpg");
// 创建字节输出流
FileOutputStream fos = new FileOutputStream("D:\\copy.jpg");
// 一次读写一个字节
// int by;
// while ((by = fis.read()) != -1) {
// fos.write(by);
// }
byte[] bys = new byte[1024];
int len;// 每次真实读到数据的个数
int by;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// 释放资源
fos.close();
}
}
IO流之复制图片
于 2023-02-13 12:42:12 首次发布