文本文件拷贝
字符流
FileReader
FileWriter
public class FileCharCopyDemo {
public static void main(String[] args) {
//1.创建FileReader与test文件关联
FileReader fr = new FileReader("/Users/gaigai/Desktop/test.txt");
//2.创建FileWriter与day文件关联
FileWriter fw = new FileWriter("/Users/gaigai/Desktop/day.txt");
System.out.println("开始拷贝");
int res = 0;
while (-1 != (res = fr.read())){
System.out.println("读取到的单个字符是:" + (char)res);
fw.write(res);
}
System.out.println("拷贝完成");
//后创建的先关闭
fw.close();
fr.close();
}
}
- 优化报错:
public class FileCharCopyDemo {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
//1.创建FileReader与test文件关联
fr = new FileReader("/Users/gaigai/Desktop/test.txt");
//2.创建FileWriter与day文件关联
fw = new FileWriter("/Users/gaigai/Desktop/day.txt");
System.out.println("开始拷贝");
int res = 0;
while (-1 != (res = fr.read())){
System.out.println("读取到的单个字符是:" + (char)res);
fw.write(res);
}
System.out.println("拷贝完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != fw){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != fr){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
图片文件拷贝
字符流
FileReader
FileWriter
- 拷贝图片失败
FileReader
,FileWriter
拷贝成功,但是拷贝的文件打开报错- 「不能拷贝除了文本文件以外的内容」
- 拷贝图片的文件大小也不一样
字节流
FileInputStream
FileOutputStream
[[04.FileputStream类]]
public class FileStreamDemo {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 1.创建FileInputStream类型的对象与d:/03 IO流的框架图.png文件关联
fis = new FileInputStream("/Users/gaigai/Desktop/1.png");
fis = new FileInputStream("/Users/gaigai/Desktop/1.png");
fos = new FileOutputStream("/Users/gaigai/Desktop/2.png");
System.out.println("开始拷贝");
int res = 0;
while (-1 != (res = fis.read())){
fos.write(res);
}
System.out.println("拷贝完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
//后创建的先关闭
if(null != fos){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != fis){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
mp4文件拷贝
字节流
FileInputStream
FileOutputStream
单个字节为单位拷贝
- 以单个字节为单位进行拷贝,也就是每次读取一个字节后再写入一个字节
- 缺点:文件稍大时,拷贝的效率很低
fis = new FileInputStream("/Users/gaigai/Desktop/a.mp4");
fos = new FileOutputStream("/Users/gaigai/Desktop/b.mp4");
int res = 0;
while (-1 != (res = fis.read())){
fos.write(res);
}
System.out.println("拷贝完成");
- 超级慢
文件缓冲区拷贝
- 准备一个和文件大小一样的缓冲区,一次性将文件中的所有内容取出到缓冲区然后一次性写入进去
- 缺点:若文件过大时,无法申请和文件大小一样的缓冲区,真实物理内存不足
//获取文件大小
int size = fis.available();
System.out.println("文件大小:"+ size);
byte[] bytes = new byte[size];
//读取到bytes数组中
int read = fis.read(bytes);
System.out.println("实际读取到的文件大小是:" + read);
fos.write(bytes);
如果是30本 50本书的情况下可以,如果是30万本书,怎么办?一次性拿什么承载呢?
- 准备一个相对适当的缓冲区
//准备一个相对适当的缓冲区,分多次将文件拷贝完成
byte[] bArr = new byte[1024];
int res = 0;
while ((res = fis.read(bArr)) != -1) {
fos.write(bArr, 0, res);
}
[[05.BufferedStream]]
import java.io.*;
public class BufferedDemo {
public static void main(String[] args) {
BufferedOutputStream buos = null;
BufferedInputStream buis = null;
try {
buis = new BufferedInputStream(new FileInputStream("/Users/gaigai/Desktop/a.mp4"));
buos = new BufferedOutputStream(new FileOutputStream("/Users/gaigai/Desktop/bb.mp4"));
System.out.println("开始拷贝");
//准备一个相对适当的缓冲区,分多次将文件拷贝完成
byte[] bArr = new byte[1024];
int res = 0;
while ((res = buis.read(bArr)) != -1) {
buos.write(bArr, 0, res);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != buos) {
try {
buos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != buis) {
try {
buis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 添加时间进行比较:
// 获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
long g1 = System.currentTimeMillis();
long g2 = System.currentTimeMillis();
System.out.println("使用缓冲流拷贝视频文件消耗的时间为:" + (g2-g1)); // 128
- 对应FileStream添加时间
// 获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
long g1 = System.currentTimeMillis();
long g2 = System.currentTimeMillis();
System.out.println("使用缓冲流拷贝视频文件消耗的时间为:" + (g2-g1)); // 128
文本文件使用缓冲
[[06.Buffered类]]
public class BufferedCharDemo {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
// 1.创建BufferedReader类型的对象与d:/a.txt文件关联
br = new BufferedReader(new FileReader("/Users/gaigai/Desktop/test.txt"));
// 2.创建BufferedWriter类型的对象与d:/b.txt文件关联
bw = new BufferedWriter(new FileWriter("/Users/gaigai/Desktop/b.txt"));
// 3.不断地从输入流中读取一行字符串并写入到输出流中
String str = null;
//null if the end of the stream has been reached without reading any characters
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine(); // 当前系统中的行分隔符是:window \r\n mac \n
}
System.out.println("拷贝文件成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭流对象并释放有关的资源
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileReader fileReader = null;
try {
//构造FileReader类型的对象与文件关联
fileReader = new FileReader("/Users/gaigai/Desktop/b.txt");
//读取数据内容并打印
int res = 0;
while (-1 != (res = fileReader.read())){
System.out.println("读取到的单个字符是:" + (char)res + ","+res);
}
}catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流对象并释放有关的资源
if (null != fileReader) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}