Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream)

          Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream)

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  在我们学习字节流与字符流的时候,大家都进行过读取文件中数据的操作,读取数据量大的文件时,读取的速度会很慢,很影响我们程序的效率,那么,我想提高速度,怎么办?Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度

  缓冲流,根据流的分类分为字节缓冲流与字符缓冲流。本篇博客主要介绍的是字节缓冲流。

一.字节缓冲流

  字节缓冲流根据流的方向,共有2个:

      1>. 写入数据到流中,字节缓冲输出流 BufferedOutputStream

扫描二维码关注公众号,回复: 97030 查看本文章

      2>.读取流中的数据,字节缓冲输入流 BufferedInputStream

  它们的内部都包含了一个缓冲区,通过缓冲区读写,就可以提高了IO流的读写速度

 

二.字节输出缓冲流

  java.io.BufferedOutputStream作用:提高原有输出流的写入效率。

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note5;
 8 
 9 import java.io.BufferedOutputStream;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 
13 public class BufferedOutputStreamDemo {
14     public static void main(String[] args) throws IOException {
15         //创建字节输出流,绑定文件
16         FileOutputStream fos = new FileOutputStream("yinzhengjie.txt");
17         //创建字节输出流缓冲流对象,构造方法中,传递字节输出流,实这里也可以直接匿名生成。
18         BufferedOutputStream bos = new BufferedOutputStream(fos);    
19         //写入一个字节
20         bos.write(97);
21         //将字符串变成字节数组
22         byte[] buf = "yinzhengjie".getBytes();     
23         //写一个字节数组
24         bos.write(buf);
25         //写入字节数组的一部分
26         bos.write(buf, 3, 5);
27         //释放资源,我们不用关闭fos对象,因为关闭bos对象会自动关闭fos流。
28         bos.close();
29     }
30 }

三.字节输入缓冲流

  java.io.BufferedInputStream作用:提高原有输入流的读取效率。

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note5;
 8 
 9 import java.io.BufferedInputStream;
10 import java.io.FileInputStream;
11 import java.io.IOException;
12 
13 public class BufferedInputStreamDemo {
14     public static void main(String[] args) throws IOException {
15         //创建字节输入流,包装文件
16         FileInputStream fis = new FileInputStream("yinzhengjie.txt");
17         //创建字节输入流缓冲流对象,构造方法中包装字节输入流,包装文件
18         BufferedInputStream bis = new BufferedInputStream(fis);
19         //按照字节数组的方式进行读取操作,此处我并没有按照单个字节方式进行读取。
20         byte[] buf = new byte[4096];
21         int len;
22         while(( len = bis.read(buf)) != -1 ) {
23             System.out.println(new String(buf,0,len));
24         }
25         //释放资源,此处还是不用关闭fis流对象。
26         bis.close();
27     }
28 }
29 
30 
31 /*
32 以上代码执行结果如下:
33 ayinzhengjiezheng
34 */

四.四种文件复制方式的效率比较

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note5;
 8 
 9 import java.io.BufferedInputStream;
10 import java.io.BufferedOutputStream;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 
16 public class CopyFile {
17     public static void main(String[] args) throws IOException {
18         
19         long start = System.currentTimeMillis();
20 //        copy1(new File("yinzhengjie.sql"),new File("yinzhengjie.sql.backup"));
21 //        copy2(new File("yinzhengjie.sql"),new File("yinzhengjie.sql.backup"));
22 //        copy3(new File("yinzhengjie.sql"),new File("yinzhengjie.sql.backup"));      //
23         copy4(new File("yinzhengjie.sql"),new File("yinzhengjie.sql.backup"));   ////拷贝的是600M的文件只需要862毫秒,效率是最高的!(固态硬盘)
24         long stop = System.currentTimeMillis();
25         System.out.println(stop-start);
26     }
27     
28     
29 
30     //1>.字节流读写单个字符。
31     public static void copy1(File src,File dest) throws IOException{
32         FileInputStream fis = new FileInputStream(src);
33         FileOutputStream fos = new FileOutputStream(dest);
34         int len;
35         while( (len = fis.read()) != -1) {
36             fos.write(len);
37         }
38         fos.close();
39         fis.close();
40     }
41     
42     //2>.字节流读写字节数组
43     public static void copy2(File src,File dest) throws IOException{
44         FileInputStream fis = new FileInputStream(src);
45         FileOutputStream fos = new FileOutputStream(dest);
46         byte[] cache = new byte[1024];
47         int len = 0;
48         while( (len = fis.read(cache)) != -1) {
49             fos.write(cache, 0, len);
50         }
51         fos.close();
52         fis.close();
53     }
54     
55     //字节流缓冲区读取单个字符
56     public static void copy3(File src,File dest) throws IOException{
57         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
58         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
59         
60         int len = 0;
61         while( (len = bis.read()) != -1 ) {
62             bos.write(len);
63         }
64         bos.close();
65         bis.close();
66     }
67     
68     //字节流缓冲区读取字符数组
69     public static void copy4(File src,File dest) throws IOException{
70         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
71         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
72         byte[] cache = new byte[1024];
73         int len;
74         while((len = bis.read(cache)) != -1 ) {
75             bos.write(cache, 0, len);
76         }
77     }
78     
79 }

猜你喜欢

转载自www.cnblogs.com/yinzhengjie/p/8977985.html