欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java中的包装流和缓冲流概述

程序员文章站 2022-04-03 19:35:05
...

处理流/包装流(相对于节点流更高级)装饰设计模式/包装模式:

1:隐藏了底层的节点流的差异,并对外提供了更方便的输入/输出功能,让我们只关心高级流的操作.
2:使用处理流包装了节点流,程序直接操作处理流,让节点流与底层的设备做IO操作.
3:只需要关闭处理流即可.
包装流如何区分:写代码的时候,发现创建对象对象的时候,需要传递另一个流对象.
new 包装流( 流对象 ) ;

什么是缓冲流:

是一个包装流,目的起缓冲作用.
 BufferedInputStream:
 BufferedOutputStream:
 BufferedReader:
 BufferedWriter:

缓冲流的目的:

操作流的时候,习惯定义一个byte/char数组.
int read():每次都从磁盘文件中读取一个字节. 直接操作磁盘文件性能极低.
解决方案: 定义一个数组作为缓冲区.
byte[] buffer = new byte[1024]; 该数组其实就是一个缓冲区.
一次性从磁盘文件中读取1024个字节. 如此以来,操作磁盘文件的次数少了,—->性能得以提升.
既然我们都能想到,SUN公司早就想到了,提供的默认缓存区大小是8192(1024*8),我们一般不用修改大小.
java中的包装流和缓冲流概述

代码:

字节缓冲流案例:

package IO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class BufferStreamDemo {
    public static void main(String[] args) throws Exception {
        File file = new File("file/aaa.txt");
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        out.write("中国".getBytes());
        out.close();
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[1024];
        int len = -1;
        while((len = in.read(buffer)) != -1){
            System.out.println(new String(buffer, 0, len));
        }
        in.close();
    }
}

字符缓冲流案例:

package IO;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;

public class BufferCharacterDemo {
    public static void main(String[] args) throws Exception {
        File file = new File("file/aaa.txt");
        BufferedWriter in = new BufferedWriter(new FileWriter(file,true));
        in.newLine();//用来换行等同于‘\n’
        in.write("美国");
        in.newLine();
        in.write("马来西亚");
        in.close();
        BufferedReader out = new BufferedReader(new FileReader(file));
        String line = null;
        //按行读取
        while((line = out.readLine()) != null){
            System.out.println(line);
        }
        out.close();
    }
}

节点流和缓冲流性能对比:

操作字节和字符流都习惯使用缓冲流给包装起来,提供IO性能/效率.

package IO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class NodeStreamVSBufferStream {
    public static void main(String[] args) throws Exception {
        //File file = new File("file/0001.酷六网-余光中《写给未来的你》(曹可扬朗诵)[流畅版].flv");
        File file = new File("file/aaa.txt");
        File copyFile = new File("file/bbb.txt");
        printContent(file);
        //File copyFile = new File("file/《写给未来的你》.flv");
        test1(file,copyFile);
        test2(file,copyFile);
        test3(file,copyFile);
        test4(file,copyFile);
    }

    private static void printContent(File file) throws Exception {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        byte[] b = new byte[1024];
        int len = -1;
        while((len = in.read(b)) != -1){
            System.out.println(new String(b));
        }

    }

    private static void test4(File file, File copyFile) throws Exception {
        //测试包装流 ,1024个字节拷贝
        System.out.println("------------测试包装流 ,1024个字节拷贝---------------");
        long start = System.currentTimeMillis();

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(copyFile));
        int len = -1;
        byte[] b = new byte[1024];
        while((len = in.read(b))!= -1){
            out.write(b);
        }
        in.close();
        out.close();
        System.out.println(System.currentTimeMillis() - start);
    }

    private static void test3(File file, File copyFile) throws Exception{
        //测试包装流 ,单个个字节拷贝
        System.out.println("------------测试包装流 ,单个个字节拷贝---------------");
        long start = System.currentTimeMillis();
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(copyFile));
        int len = -1;
        while((len = in.read())!= -1){
            out.write(len);
        }
        in.close();
        out.close();
        System.out.println(System.currentTimeMillis() - start);
    }

    private static void test2(File file, File copyFile) throws Exception {
        //测试字节流 ,存1024个字节拷贝
        System.out.println("------------测试字节流 ,存1024个字节拷贝---------------");
        long start = System.currentTimeMillis();
        InputStream in = new FileInputStream(file);
        OutputStream out = new FileOutputStream(copyFile);
        int len = -1;
        byte[] b = new byte[1024];
        while((len = in.read(b))!= -1){
            out.write(b);
        }
        in.close();
        out.close();
        System.out.println(System.currentTimeMillis() - start);
    }


    private static void test1(File file, File copyFile) throws Exception {
        //测试一个字节一个字节拷贝
        System.out.println("-----------测试一个字节一个字节拷贝----------------");
        long start = System.currentTimeMillis();
        InputStream in = new FileInputStream(file);
        OutputStream out = new FileOutputStream(copyFile);
        int len = -1;
        while((len = in.read())!= -1){
            out.write(len);
        }
        in.close();
        out.close();
        System.out.println(System.currentTimeMillis() - start);
    }
}
相关标签: 包装流 缓冲流