package task17mofang;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileCharCopyTest {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(“d:/a.txt”);
fw = new FileWriter(“d:/b.txt”);
System.out.println(“正在玩命的拷贝…”);
int res = 0;
while ((res = fr.read())!=-1){
fw.write(res);
}
System.out.println(“拷贝文件成功”);
} catch (IOException e) {
e.printStackTrace();
} finally {
//先建立的后关闭
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符流仅仅可以处理文本类的
字节流可以处理所有文件
复制视频
package task17mofang;
import java.io.*;
public class BufferedByteCopyTest {
public static void main(String[] args) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.创建BufferedInputStream类型的对象与e:/桌面右侧/哈哈/WeChat_20210330161303.mp4
//的文件关联
bis = new BufferedInputStream(new FileInputStream(“e:/桌面右侧/哈哈/WeChat_20210330161303.mp4”));
//2.创建BufferedOutputStream类型的对象与e:/桌面右侧/哈哈/WeChat_20210330161303.mp4
//的文件关联
bos = new BufferedOutputStream(new FileOutputStream(“e:/桌面右侧/哈哈/WeChat.mp4”));
//3.不断的从输入流读取数据并写入到输出流中
System.out.println(“开始拷贝”);
byte[] bArr = new byte[1024];
int res = 0;
//读入 写出
while ((res = bis.read(bArr))!=-1){
bos.write(bArr,0,res);
}
System.out.println(“拷贝文件成功”);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}