其他分享
首页 > 其他分享> > IO缓冲流

IO缓冲流

作者:互联网

IO缓冲流

字节缓冲输入输出流

创建缓冲区,提高性能

import java.io.*;

public class Buffer {
    public static void main(String[] args) throws IOException {
        // 字节缓冲输入流构造方法:public BufferedInputStream(InputStream in)
        // 字节缓冲输出流构造方法:public BufferedOutputStream(OutputStream out)

        // new一个FileOutputStream类对象
//        FileOutputStream fileOutputStream = new FileOutputStream("java基础\\src\\com\\io\\buffer\\img.jpg");
//        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        // 合并为一步
//        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("java基础\\src\\com\\io\\buffer\\java.txt"));

        // 写入数据
//         bufferedOutputStream.write("hello\r\n".getBytes());
//         bufferedOutputStream.write("world".getBytes());

        // new一个FileInputStream类对象
//        FileInputStream fileInputStream = new FileInputStream("java基础\\src\\com\\io\\buffer\\img.jpg");
//        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

        // 合并为一步
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("java基础\\src\\com\\io\\buffer\\java.txt"));

        // 一次读取一个字节数据
//        int read;
//        while ((read = bufferedInputStream.read()) != -1){
//            System.out.print((char)read);
//        }


        // 一次读取一个字节数组
        byte[] bytes = new byte[1024];
        int read;
        while ((read = bufferedInputStream.read(bytes)) != -1){
            System.out.print(new String(bytes,0,read));
        }

        // 释放资源
//        bufferedOutputStream.close();
        bufferedInputStream.close();
    }
}

标签:java,字节,read,缓冲,IO,BufferedOutputStream,new,public
来源: https://www.cnblogs.com/coderDreams/p/15846263.html