java笔记之IO4文本复制

* 复制文本文件。
 *
 * 数据源:从哪里来
 * a.txt -- 读取数据 -- FileInputStream 
 *
 * 目的地:到哪里去
 * b.txt -- 写数据  -- FileOutputStream
 *
 * java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)
 *
 * 这一次复制中文没有出现任何问题,为什么呢?
 * 上一次我们出现问题的原因在于我们每次获取到一个字节数据,就把该字节数据转换为了字符数据,然后输出到控制台。
 * 而这一次呢?确实通过IO流读取数据,写到文本文件,你读取一个字节,我就写入一个字节,你没有做任何的转换。
 * 它会自己做转换。

 1 public class CopyFileDemo {
 2     public static void main(String[] args) throws IOException {
 3         // 封装数据源
 4         FileInputStream fis = new FileInputStream("a.txt");
 5         // 封装目的地
 6         FileOutputStream fos = new FileOutputStream("b.txt");
 7 
 8         int by = 0;
 9         while ((by = fis.read()) != -1) {
10             fos.write(by);
11         }
12 
13         // 释放资源(先关谁都行)
14         fos.close();
15         fis.close();
16     }
17 }
View Code

* 通过定义数组的方式确实比以前一次读取一个字节的方式快很多,所以,看来有一个缓冲区还是非常好的。
 * 既然是这样的话,那么,java开始在设计的时候,它也考虑到了这个问题,就专门提供了带缓冲区的字节类。
 * 这种类被称为:缓冲区类(高效类)
 * 写数据:BufferedOutputStream
 * 读数据:BufferedInputStream
 *
 * 构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。
 *
 * 为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
 * 原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。

 1 public class BufferedOutputStreamDemo {
 2     public static void main(String[] args) throws IOException {
 3         // BufferedOutputStream(OutputStream out)
 4         // FileOutputStream fos = new FileOutputStream("bos.txt");
 5         // BufferedOutputStream bos = new BufferedOutputStream(fos);
 6         // 简单写法
 7         BufferedOutputStream bos = new BufferedOutputStream(
 8                 new FileOutputStream("bos.txt"));
 9 
10         // 写数据
11         bos.write("hello".getBytes());
12 
13         // 释放资源
14         bos.close();
15     }
16 }
View Code

 * 字节流四种方式复制文件:
 * 基本字节流一次读写一个字节: 共耗时:117235毫秒
 * 基本字节流一次读写一个字节数组: 共耗时:156毫秒
 * 高效字节流一次读写一个字节: 共耗时:1141毫秒
 * 高效字节流一次读写一个字节数组: 共耗时:47毫秒

 1 public class CopyMp4Demo {
 2     public static void main(String[] args) throws IOException {
 3         long start = System.currentTimeMillis();
 4         // method1("e:\哥有老婆.mp4", "copy1.mp4");
 5         // method2("e:\哥有老婆.mp4", "copy2.mp4");
 6         // method3("e:\哥有老婆.mp4", "copy3.mp4");
 7         method4("e:\哥有老婆.mp4", "copy4.mp4");
 8         long end = System.currentTimeMillis();
 9         System.out.println("共耗时:" + (end - start) + "毫秒");
10     }
11 
12     // 高效字节流一次读写一个字节数组:
13     public static void method4(String srcString, String destString)
14             throws IOException {
15         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
16                 srcString));
17         BufferedOutputStream bos = new BufferedOutputStream(
18                 new FileOutputStream(destString));
19 
20         byte[] bys = new byte[1024];
21         int len = 0;
22         while ((len = bis.read(bys)) != -1) {
23             bos.write(bys, 0, len);
24         }
25 
26         bos.close();
27         bis.close();
28     }
29 
30     // 高效字节流一次读写一个字节:
31     public static void method3(String srcString, String destString)
32             throws IOException {
33         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
34                 srcString));
35         BufferedOutputStream bos = new BufferedOutputStream(
36                 new FileOutputStream(destString));
37 
38         int by = 0;
39         while ((by = bis.read()) != -1) {
40             bos.write(by);
41 
42         }
43 
44         bos.close();
45         bis.close();
46     }
47 
48     // 基本字节流一次读写一个字节数组
49     public static void method2(String srcString, String destString)
50             throws IOException {
51         FileInputStream fis = new FileInputStream(srcString);
52         FileOutputStream fos = new FileOutputStream(destString);
53 
54         byte[] bys = new byte[1024];
55         int len = 0;
56         while ((len = fis.read(bys)) != -1) {
57             fos.write(bys, 0, len);
58         }
59 
60         fos.close();
61         fis.close();
62     }
63 
64     // 基本字节流一次读写一个字节
65     public static void method1(String srcString, String destString)
66             throws IOException {
67         FileInputStream fis = new FileInputStream(srcString);
68         FileOutputStream fos = new FileOutputStream(destString);
69 
70         int by = 0;
71         while ((by = fis.read()) != -1) {
72             fos.write(by);
73         }
74 
75         fos.close();
76         fis.close();
77     }
78 }
View Code
原文地址:https://www.cnblogs.com/lanjianhappy/p/6381341.html