零基础学习java------day17------字节缓冲流

时间:2019-08-24
本文章向大家介绍零基础学习java------day17------字节缓冲流,主要包括零基础学习java------day17------字节缓冲流使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 缓冲字节

缓冲区:缓冲区实质上是一个数组。通常它是一个字节数组,但是也可以使用其他种类的数组。但是一个缓冲区不 仅仅 是一个数组。缓冲区提供了对数据的结构化访问,而且还可以跟踪系统的读/写进程。

缓冲流出现的原因:使用字节流每次从文件中进行读写的时候,都需要和文件进行大量的IO交互,与磁盘交互的效率其实是比较低的,所以为了降低与磁盘的交互次数,可以使用字节缓冲流。字节缓冲流将数据放到缓存区,而缓冲区是一个内存区域的概念,我们直接和缓冲区做交互,可以提升效率。

注意:

(1)什么时候缓冲区的数据写入硬盘中?

     当缓冲区被写满时,或是使用flush方法将至写入硬盘(注意关流后,缓存区的内容会被写入硬盘,因为关流内部会调用flush方法)

2)byte数组的大小要小于缓存区,缓存区的数据是通过数组间接读入的

1.1 缓冲字节输出流

BufferOutputStream(OutputStream);

 1.1.1 构造方法:

(1)public BufferedOutputStream(OutputStream out)

(2)public BufferedOutputStream(OutputStream out, int size):  此处参数size表示缓冲区的大小,默认是8kb

 1.1.2 成员方法:

(1)public void write(int b)

(2)public void write(byte b[])

(3)public void write(byte b[], int off, int len):off表示偏移量,len表示从偏移量位置开始写入数据的长度

(4)public void flush(): 刷新,将缓存区的内容写到文件中,一般只有带缓冲的输出流才有这样的方法

public class BufferOutputStreamDemo {
    public static void main(String[] args) {
        try (
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:/a.txt"));
                ){
                bos.write("妈妈,他们抛弃了我".getBytes());//妈妈,他们抛弃了我
                bos.write(97); //a
                bos.write("妈妈,他们抛弃了我".getBytes(),0,6);//妈妈
                bos.flush();  //一般使用输出流的时候,尽量把flish写出来
     }
catch (Exception e) { e.printStackTrace(); } } }

注意,此处若没用自动关流,由于缓存区的内存没被写满,所以内容不会被写进a.txt

 1.2 缓冲字节输入流

 BufferedInputStream(InputStream)

  BufferedInputStream(InputStream)
  BufferedInputStream(InputStream,int size) size: 缓冲区大小,默认8k

 其读取数据的方法和FileInputStream是一样的(见上)

public class BufferedInputStreamDemo {
    public static void main(String[] args) {
        try (
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("e:/b.txt"));// b.txt中的内容为:这个世界会好吗
                ){
            byte[] bs = new byte[1024];
            int len ;
            while((len = bis.read(bs)) != -1) {  //判断数据读完的条件
                System.out.println(new String(bs,0,len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

练习:使用BufferedOutputStream/BufferedInputStream拷贝文件,并比较和FileInput的拷贝性能

 1 public class CopyFile {
 2     public static void fileStream(String srcPath,String destPath) {
 3         long start = System.currentTimeMillis();
 4         try(
 5                 FileInputStream fis = new FileInputStream(srcPath);
 6                 FileOutputStream fos = new FileOutputStream(destPath);
 7                 ) {
 8             byte[] bs = new byte[1024];
 9             int len;
10             while((len = fis.read(bs)) != -1) {
11                 fos.write(bs,0,len);
12             }
13             long end = new Date().getTime();
14             System.out.println("字节流耗时为:"+(end-start)+"毫秒");
15         } catch (Exception e) {
16             e.printStackTrace();
17         }
18     }
19 public static void bufferedFileStream(String srcPath,String destPath) {
20     long start = System.currentTimeMillis();
21     try (
22             BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcPath));
23             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
24             ){
25         int len;
26         byte[] bs = new byte[1024];
27         while((len = bis.read(bs)) != -1) {
28             bos.write(bs,0,len);
29         }
30         long end = new Date().getTime();
31         System.out.println("缓冲字节流耗时为:"+(end-start)+"毫秒");
32     } catch (Exception e) {
33         e.printStackTrace();
34         }
35     }
36 }
View Code

运行结果是:缓冲字节流与字节流拷贝同一个文件,前者花了95毫秒,后者花了405毫秒,可见缓冲字节流效率很高

原文地址:https://www.cnblogs.com/jj1106/p/11406436.html