文件拷贝 - 图1

文本文件拷贝

字符流

  • FileReader
  • FileWriter
  1. public class FileCharCopyDemo {
  2. public static void main(String[] args) {
  3. //1.创建FileReader与test文件关联
  4. FileReader fr = new FileReader("/Users/gaigai/Desktop/test.txt");
  5. //2.创建FileWriter与day文件关联
  6. FileWriter fw = new FileWriter("/Users/gaigai/Desktop/day.txt");
  7. System.out.println("开始拷贝");
  8. int res = 0;
  9. while (-1 != (res = fr.read())){
  10. System.out.println("读取到的单个字符是:" + (char)res);
  11. fw.write(res);
  12. }
  13. System.out.println("拷贝完成");
  14. //后创建的先关闭
  15. fw.close();
  16. fr.close();
  17. }
  18. }
  • 优化报错:
  1. public class FileCharCopyDemo {
  2. public static void main(String[] args) {
  3. FileReader fr = null;
  4. FileWriter fw = null;
  5. try {
  6. //1.创建FileReader与test文件关联
  7. fr = new FileReader("/Users/gaigai/Desktop/test.txt");
  8. //2.创建FileWriter与day文件关联
  9. fw = new FileWriter("/Users/gaigai/Desktop/day.txt");
  10. System.out.println("开始拷贝");
  11. int res = 0;
  12. while (-1 != (res = fr.read())){
  13. System.out.println("读取到的单个字符是:" + (char)res);
  14. fw.write(res);
  15. }
  16. System.out.println("拷贝完成");
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. if(null != fw){
  21. try {
  22. fw.close();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. if(null != fr){
  28. try {
  29. fr.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35. }
  36. }

图片文件拷贝

字符流

  • FileReader
  • FileWriter
  • 拷贝图片失败
  • FileReader,FileWriter拷贝成功,但是拷贝的文件打开报错
    • 「不能拷贝除了文本文件以外的内容」
  • 拷贝图片的文件大小也不一样

字节流

  • FileInputStream
  • FileOutputStream
    [[04.FileputStream类]]
  1. public class FileStreamDemo {
  2. public static void main(String[] args) {
  3. FileInputStream fis = null;
  4. FileOutputStream fos = null;
  5. try {
  6. // 1.创建FileInputStream类型的对象与d:/03 IO流的框架图.png文件关联
  7. fis = new FileInputStream("/Users/gaigai/Desktop/1.png");
  8. fis = new FileInputStream("/Users/gaigai/Desktop/1.png");
  9. fos = new FileOutputStream("/Users/gaigai/Desktop/2.png");
  10. System.out.println("开始拷贝");
  11. int res = 0;
  12. while (-1 != (res = fis.read())){
  13. fos.write(res);
  14. }
  15. System.out.println("拷贝完成");
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. } finally {
  19. //后创建的先关闭
  20. if(null != fos){
  21. try {
  22. fos.close();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. if(null != fis){
  28. try {
  29. fis.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35. }
  36. }

mp4文件拷贝

字节流

  • FileInputStream
  • FileOutputStream

单个字节为单位拷贝

  • 以单个字节为单位进行拷贝,也就是每次读取一个字节后再写入一个字节
  • 缺点:文件稍大时,拷贝的效率很低
  1. fis = new FileInputStream("/Users/gaigai/Desktop/a.mp4");
  2. fos = new FileOutputStream("/Users/gaigai/Desktop/b.mp4");
  3. int res = 0;
  4. while (-1 != (res = fis.read())){
  5. fos.write(res);
  6. }
  7. System.out.println("拷贝完成");
  • 超级慢

文件缓冲区拷贝

  • 准备一个和文件大小一样的缓冲区,一次性将文件中的所有内容取出到缓冲区然后一次性写入进去
  • 缺点:若文件过大时,无法申请和文件大小一样的缓冲区,真实物理内存不足
  1. //获取文件大小
  2. int size = fis.available();
  3. System.out.println("文件大小:"+ size);
  4. byte[] bytes = new byte[size];
  5. //读取到bytes数组中
  6. int read = fis.read(bytes);
  7. System.out.println("实际读取到的文件大小是:" + read);
  8. fos.write(bytes);

如果是30本 50本书的情况下可以,如果是30万本书,怎么办?一次性拿什么承载呢?

  • 准备一个相对适当的缓冲区
  1. //准备一个相对适当的缓冲区,分多次将文件拷贝完成
  2. byte[] bArr = new byte[1024];
  3. int res = 0;
  4. while ((res = fis.read(bArr)) != -1) {
  5. fos.write(bArr, 0, res);
  6. }

[[05.BufferedStream]]

  1. import java.io.*;
  2. public class BufferedDemo {
  3. public static void main(String[] args) {
  4. BufferedOutputStream buos = null;
  5. BufferedInputStream buis = null;
  6. try {
  7. buis = new BufferedInputStream(new FileInputStream("/Users/gaigai/Desktop/a.mp4"));
  8. buos = new BufferedOutputStream(new FileOutputStream("/Users/gaigai/Desktop/bb.mp4"));
  9. System.out.println("开始拷贝");
  10. //准备一个相对适当的缓冲区,分多次将文件拷贝完成
  11. byte[] bArr = new byte[1024];
  12. int res = 0;
  13. while ((res = buis.read(bArr)) != -1) {
  14. buos.write(bArr, 0, res);
  15. }
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. } finally {
  19. if (null != buos) {
  20. try {
  21. buos.close();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. if (null != buis) {
  27. try {
  28. buis.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. }
  35. }
  • 添加时间进行比较:
  1. // 获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
  2. long g1 = System.currentTimeMillis();
  3. long g2 = System.currentTimeMillis();
  4. System.out.println("使用缓冲流拷贝视频文件消耗的时间为:" + (g2-g1)); // 128
  • 对应FileStream添加时间
  1. // 获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
  2. long g1 = System.currentTimeMillis();
  3. long g2 = System.currentTimeMillis();
  4. System.out.println("使用缓冲流拷贝视频文件消耗的时间为:" + (g2-g1)); // 128

文件拷贝 - 图2

文本文件使用缓冲

[[06.Buffered类]]

  1. public class BufferedCharDemo {
  2. public static void main(String[] args) {
  3. BufferedReader br = null;
  4. BufferedWriter bw = null;
  5. try {
  6. // 1.创建BufferedReader类型的对象与d:/a.txt文件关联
  7. br = new BufferedReader(new FileReader("/Users/gaigai/Desktop/test.txt"));
  8. // 2.创建BufferedWriter类型的对象与d:/b.txt文件关联
  9. bw = new BufferedWriter(new FileWriter("/Users/gaigai/Desktop/b.txt"));
  10. // 3.不断地从输入流中读取一行字符串并写入到输出流中
  11. String str = null;
  12. //null if the end of the stream has been reached without reading any characters
  13. while ((str = br.readLine()) != null) {
  14. bw.write(str);
  15. bw.newLine(); // 当前系统中的行分隔符是:window \r\n mac \n
  16. }
  17. System.out.println("拷贝文件成功!");
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. } finally {
  21. // 4.关闭流对象并释放有关的资源
  22. if (null != bw) {
  23. try {
  24. bw.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. if (null != br) {
  30. try {
  31. br.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37. }
  38. }
  1. FileReader fileReader = null;
  2. try {
  3. //构造FileReader类型的对象与文件关联
  4. fileReader = new FileReader("/Users/gaigai/Desktop/b.txt");
  5. //读取数据内容并打印
  6. int res = 0;
  7. while (-1 != (res = fileReader.read())){
  8. System.out.println("读取到的单个字符是:" + (char)res + ","+res);
  9. }
  10. }catch (IOException e) {
  11. e.printStackTrace();
  12. } finally {
  13. //关闭流对象并释放有关的资源
  14. if (null != fileReader) {
  15. try {
  16. fileReader.close();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }