The Java stream buffer (byte / character buffer stream)

Outline

Buffered stream, also known as high flow, is substantially enhanced FileXxx 4 stream, the stream is 4, according to the data type classification:
  • Byte stream buffer : BufferedInputStream, BufferedOutputStream
  • Character buffer stream : BufferedReader, BufferedWriter

The basic principles and benefits of buffer flow

  • When you create a stream object is, creates a built-in buffer array default size of the buffer by reading and writing, to reduce the number of system IO, to improve the efficiency of reading and writing.

BufferedOutputStream byte buffered output stream

Common method java.io.BufferedOutputStream extends OutputStream members inherited from the parent class:

  • public void close (): Close this output stream and releases any system resources associated with the stream.
  • public void flush (): Flushes this output stream and forces any buffered output bytes to be written.
  • public void write (byte [] b): The b.length byte to this output stream from the specified byte array.
  • Write void public (byte [] B, int off, int len) : writing from the specified byte array len bytes starting at offset off to this output from the output stream.
  • public abstract void write (int b): the specified output stream of bytes.

Common construction method

  • BufferedOutputStream (OutputStream out) Create a new buffered output stream to write data to the specified underlying output stream.
  • BufferedOutputStream (OutputStream out, int size) Create a new buffered output stream, the data is written with the specified buffer size specified underlying output stream.
Constructor parameters:
  • OutputStream out: output stream of bytes, we can pass FileOutputStream, the buffer flow will increase FileOutputStream a buffer, improving the writing efficiency FileOutputStream.
  • int size: Specifies the size of the internal buffer of the stream buffer, is not specified the default

Use step (emphasis)

  1. Create a FileOutputStream object constructor bound to be output destination
  2. Creating BufferedOutputStream object constructor passing FileOutputStream object object FileOutputStream object to improve efficiency
  3. Use write BufferedOutputStream object, the data is written into the internal buffer
  4. Use flush BufferedOutputStream object, the internal data buffer, a refresh to a file
  5. The release of resources (will be called first flush method to refresh the data, the first four may be omitted)

Code demonstrates:

Package demo01; 

Import java.io.BufferedOutputStream;
 Import java.io.FileOutputStream;
 Import java.io.IOException; 

public  class Demo01BufferedOutputStream {
     public  static  void main (String [] args) throws IOException {
         // create FileOutputStream object, specify output destination 
        FileOutputStream fos = new new FileOutputStream ( "\\ atxt day21." );
         // 2. Create BufferedOutputStream object constructor FileOutputStream object passing objects, improving the efficiency of FileOutputStream object 
        BufferedOutputStream BOS = new new BufferedOutputStream (fos);
        // write data into the internal buffer 
        bos.write ( "reading for the rise of China --- Enlai" .getBytes ()); // string into a byte array 
        / * 
        use flush BufferedOutputStream object , the internal data buffer, a refresh to a file 
        bos.flush (); 
        5. the release resources (first call flush will refresh the data, the fourth part may be omitted) 
        * / 
        bos.close (); 
    } 
}

The result of code execution

BufferedInputStream: byte buffered input stream

java.io.BufferedInputStream InputStream extends inherited from the parent class member method:

  • int read () Reads the next byte of data from the input stream.
  • int read (byte [] b) a number of bytes read from the input stream, and stores it in the buffer array b.
  • void close () Closes this input stream and releases any system resources associated with the stream.

Common constructor:

  • BufferedInputStream (InputStream in) Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
  • BufferedInputStream (InputStream in, int size) BufferedInputStream created with the specified buffer size and saves its argument, i.e., the input stream in, for later use.

Constructor parameters:

  • InputStream in: the input byte stream object, we can pass FileInputStream, the buffer flow will increase FileInputStream a buffer, to improve the reading efficiency of FileInputStream
  • int size: specifies the size of the internal buffer of the buffer flow, do not specify the default size.

Use step (emphasis):

  1. Create a FileInputStream object constructor to bind the data source to read
  2. BufferedInputStream created object constructor FileInputStream passing objects, improving the efficiency of the read object FileInputStream
  3. Use read BufferedInputStream object, to read the file
  4. Release resources

Code demonstrates:

First ready to read the file (byte file commonly used letters demonstrate, Chinese easily garbled)

 Code

package demo01;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Demo01BufferedInputStream {
    public static void main(String[] args) throws IOException {
        //1.创建FileInputStream对象,构造方法中绑定要读取的数据源
        FileInputStream fis = new FileInputStream("day21\\b.txt");
        //2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率
        BufferedInputStream bis = new BufferedInputStream(fis);
        //3.使用BufferedInputStream对象中的方法read,读取文件
        int len = 0;//记录每次读取的字节
        //从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
        byte[] b = new byte[1024];//定义数组,存储每次读取的数据
        //循环读取
        while ((len = bis.read(b)) != -1) {
            //输出读取的数据,并且转化为字符串
            System.out.println(new String(b, 0, len));
        }
        //释放资源
        bis.close();
    }
}

代码执行后的结果

利用缓冲流复制文件

代码演示

package demo01;


import java.io.*;

public class BufferedDemo {
    public static void main(String[] args) throws FileNotFoundException {
        // 记录开始时间
        long start = System.currentTimeMillis();
        // 创建流对象
        try (
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\1.txt"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D\\copy.exe"));
        ) {
            // 读写数据
            int len;
            byte[] bytes = new byte[8 * 1024];
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 记录结束时间
        long end = System.currentTimeMillis();
        System.out.println("缓冲流使用数组复制时间:" + (end - start) + " 毫秒");
    }
}

BufferedWriter:字符缓冲输出流

java.io.BufferedWriter extends Writer继承自父类的共性成员方法:

  • void write(int c) 写入单个字符。
  • void write(char[] cbuf)写入字符数组。
  • abstract void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len写的字符个数。
  • void write(String str)写入字符串。
  • void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
  • void flush()刷新该流的缓冲。
  • void close() 关闭此流,但要先刷新它

常用构造方法:

  • BufferedWriter(Writer out) 创建一个使用默认大小输出缓冲区的缓冲字符输出流。
  • BufferedWriter(Writer out, int sz) 创建一个使用给定大小输出缓冲区的新缓冲字符输出流。

构造方法参数:

  • Writer out:字符输出流,我们可以传递FileWriter,缓冲流会给FileWriter增加一个缓冲区,提高FileWriter的写入效率
  • int sz:指定缓冲区的大小,不写默认大小

特有的成员方法:

  • void newLine() 写入一个行分隔符。会根据不同的操作系统,获取不同的行分隔符

使用步骤:

  1. 创建字符缓冲输出流对象,构造方法中传递字符输出流
  2. 调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
  3. 调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据,刷新到文件中
  4. 释放资源

代码演示

package demo01;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Demo01BufferedWriter {
    public static void main(String[] args) throws IOException {
        //1.创建字符缓冲输出流对象,构造方法中传递字符输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("day21\\c.txt"));
        //2.调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
        for (int i = 0; i < 5; i++) {
            //利用for循环写5遍,中国崛起
            bw.write("中国崛起" + i);
            //特意方法换行
            bw.newLine();
        }
        //释放资源
        bw.close();
    }
}

代码执行后的结果

BufferedReader:字符缓冲输入流

java.io.BufferedReader extends Reader继承自父类的共性成员方法:

  • int read() 读取单个字符并返回。
  • int read(char[] cbuf)一次读取多个字符,将字符读入数组。
  • void close() 关闭该流并释放与之关联的所有资源。

构造方法:

  • BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。
  • BufferedReader(Reader in, int size) 创建一个使用指定大小输入缓冲区的缓冲字符输入流。

参数:

  • Reader in:字符输入流我们可以传递FileReader,缓冲流会给FileReader增加一个缓冲区,提高FileReader的读取效率
  • int size:指定缓冲区大小

特有的成员方法:

String readLine() 读取一个文本行。读取一行数据行的终止符号。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行(\r\n)。返回值:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null

使用步骤:

  1. 创建字符缓冲输入流对象,构造方法中传递字符输入流
  2. 使用字符缓冲输入流对象中的方法read/readLine读取文本
  3. 释放资源

代码演示

package demo01;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Demo01BufferedReader {
    public static void main(String[] args) throws IOException {
        //1.创建字符缓冲输入流对象,构造方法中传递字符输入流\
        BufferedReader br = new BufferedReader(new FileReader("day21\\c.txt"));
         /*
            发下以上读取是一个重复的过程,所以可以使用循环优化
            不知道文件中有多少行数据,所以使用while循环
            while的结束条件,读取到null结束
         */
        String s; //定义变量,记录每次读取的数据
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
        //释放资源
        br.close();

    }
}

代码执行的结果

练习:文本排序

 代码实现

package demo01;

import java.io.*;
import java.util.HashMap;

/*
    练习:
        对文本的内容进行排序
        按照(1,2,3....)顺序排序
    分析:
        1.创建一个HashMap集合对象,可以:存储每行文本的序号(1,2,3,..);value:存储每行的文本
        2.创建字符缓冲输入流对象,构造方法中绑定字符输入流
        3.创建字符缓冲输出流对象,构造方法中绑定字符输出流
        4.使用字符缓冲输入流中的方法readline,逐行读取文本
        5.对读取到的文本进行切割,获取行中的序号和文本内容
        6.把切割好的序号和文本的内容存储到HashMap集合中(key序号是有序的,会自动排序1,2,3,4..)
        7.遍历HashMap集合,获取每一个键值对
        8.把每一个键值对,拼接为一个文本行
        9.把拼接好的文本,使用字符缓冲输出流中的方法write,写入到文件中
        10.释放资源
 */
public class Test {
    public static void main(String[] args) throws IOException {
        //1.创建一个HashMap集合对象,可以:存储每行文本的序号(1,2,3,..);value:存储每行的文本
        HashMap<String, String> map = new HashMap<>();
        //2.创建字符缓冲输入流对象,构造方法中绑定字符输入流
        BufferedReader br = new BufferedReader(new FileReader("day20\\a.txt"));
        //3.创建字符缓冲输出流对象,构造方法中绑定字符输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("day20\\b.txt"));
        //4.使用字符缓冲输入流中的方法readline,逐行读取文本
        String line;
        while ((line = br.readLine()) != null) {
            //5.对读取到的文本进行切割,获取行中的序号和文本内容
            String[] arr = line.split("\\.");
            //6.把切割好的序号和文本的内容存储到HashMap集合中(key序号是有序的,会自动排序1,2,3,4..)
            map.put(arr[0], arr[1]);
        }
        //7.遍历HashMap集合,获取每一个键值对
        for (String key : map.keySet()) {
            String value = map.get(key);
            //8.把每一个键值对,拼接为一个文本行
            line = key + "." + value;
            //9.把拼接好的文本,使用字符缓冲输出流中的方法write,写入到文件中
            bw.write(line);
            bw.newLine();//写换行
        }
        //10.释放资源
        bw.close();
        br.close();
    }
}

 

 

Guess you like

Origin www.cnblogs.com/wurengen/p/12079574.html