JAVA buffer stream combined byte stream and character stream application example (with code)

BufferedOutputStream: byte buffered output stream

1. java.io.BufferedOutputStream extends OutputStream
		BufferedOutputStream:字节缓冲输出流

    2. 继承自父类的共性成员方法:
        - public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
        - public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
        - public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。
        - public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。
        - public abstract void write(int b) :将指定的字节输出流。

     3. 构造方法:
        BufferedOutputStream(OutputStream out)  创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
        BufferedOutputStream(OutputStream out, int size)  创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
        参数:
           OutputStream out:字节输出流
                我们可以传递FileOutputStream,缓冲流会给FileOutputStream增加一个缓冲区,提高FileOutputStream的写入效率
           int size:指定缓冲流内部缓冲区的大小,不指定默认
     4. 使用步骤(重点)
        1.创建FileOutputStream对象,构造方法中绑定要输出的目的地
        2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象对象,提高FileOutputStream对象效率
        3.使用BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区中
        4.使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
        5.释放资源(会先调用flush方法刷新数据,4部可以省略)
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo01BufferedOutputStream {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileOutputStream fos = new FileOutputStream("10_IO\\a.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write("我把数据写入到内部缓冲区中".getBytes());
        bos.close();
    }

}

2 BufferedInputStream: byte buffered input stream

  1. java.io.BufferedInputStream extends InputStream
    BufferedInputStream: byte buffered input stream

  2. Member methods inherited from the parent class:
    int read() reads the next byte of data from the input stream.
    int read(byte[] b) Reads a certain number of bytes from the input stream and stores it in the buffer array b.
    void close() Close this input stream and release all system resources associated with the stream.

  3. Construction method:
    BufferedInputStream(InputStream in) Create a BufferedInputStream and save its parameters, that is, the input stream in for future use.
    BufferedInputStream(InputStream in, int size) Creates a BufferedInputStream with the specified buffer size and saves its parameters, the input stream in, for future use.
    Parameters:
    InputStream in: byte input stream
    We can pass FileInputStream, the buffered stream will add a buffer to FileInputStream to improve the reading efficiency of FileInputStream
    int size: specify the size of the internal buffer of the buffered stream, do not specify the default

  4. Use steps (emphasis):
    1. Create a FileInputStream object, bind the data source to be read in the construction method
    2. Create a BufferedInputStream object, pass the FileInputStream object in the construction method, improve the reading efficiency of the FileInputStream object
    3. Use the BufferedInputStream object Method read, read the file
    4. Release resources

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo02BufferedInputStream {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileInputStream fis = new FileInputStream("10_IO\\a.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        //int read()从输入流中读取数据的下一个字节。
        /*int len = 0;//记录每次读取到的字节
        while((len = bis.read())!=-1){
            System.out.println(len);
        }*/

        //int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
        byte[] bytes =new byte[1024];//存储每次读取的数据
        int len = 0; //记录每次读取的有效字节个数
        while((len = bis.read(bytes))!=-1){
    
    
            System.out.println(new String(bytes,0,len));
        }
        //4.释放资源
        bis.close();
    }
}

3 BufferedWriter: character buffered output stream

  1. java.io.BufferedWriter extends Writer
		BufferedWriter:字符缓冲输出流

    2. 继承自父类的共性成员方法:
        - 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() 关闭此流,但要先刷新它。

    3. 构造方法:
        BufferedWriter(Writer out) 创建一个使用默认大小输出缓冲区的缓冲字符输出流。
        BufferedWriter(Writer out, int sz) 创建一个使用给定大小输出缓冲区的新缓冲字符输出流。
        参数:
            Writer out:字符输出流
                我们可以传递FileWriter,缓冲流会给FileWriter增加一个缓冲区,提高FileWriter的写入效率
            int sz:指定缓冲区的大小,不写默认大小

    4. 特有的成员方法:
        void newLine() 写入一个行分隔符。会根据不同的操作系统,获取不同的行分隔符
        换行:换行符号
        windows:\r\n
        linux:/n
        mac:/r
    5. 使用步骤:
        1.创建字符缓冲输出流对象,构造方法中传递字符输出流
        2.调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
        3.调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据,刷新到文件中
        4.释放资源
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Demo03BufferedWriter {
    
    
    public static void main(String[] args) throws IOException {
    
    
        BufferedWriter bw = new BufferedWriter(new FileWriter("10_IO\\c.txt"));
        for (int i = 0; i <10 ; i++) {
    
    
            bw.write("传智播客");
            //bw.write("\r\n");
            bw.newLine();
        }
        bw.close();
    }
}

4 BufferedReader: character buffered input stream

1. java.io.BufferedReader extends Reader
		BufferedReader:字符缓冲输入流

    2. 继承自父类的共性成员方法:
        int read() 读取单个字符并返回。
        int read(char[] cbuf)一次读取多个字符,将字符读入数组。
        void close() 关闭该流并释放与之关联的所有资源。

    3. 构造方法:
        BufferedReader(Reader in)  创建一个使用默认大小输入缓冲区的缓冲字符输入流。
        BufferedReader(Reader in, int sz)     创建一个使用指定大小输入缓冲区的缓冲字符输入流。
        参数:
            Reader in:字符输入流
                我们可以传递FileReader,缓冲流会给FileReader增加一个缓冲区,提高FileReader的读取效率
    4. 特有的成员方法:
        String readLine() 读取一个文本行。读取一行数据
            行的终止符号:通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行(\r\n)
        返回值:
            包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null

    5. 使用步骤:
        1.创建字符缓冲输入流对象,构造方法中传递字符输入流
        2.使用字符缓冲输入流对象中的方法read/readLine读取文本
        3.释放资源
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Demo04BufferedReader {
    
    
    public static void main(String[] args) throws IOException {
    
    
        BufferedReader br = new BufferedReader(new FileReader("10_IO\\c.txt"));

        //2.使用字符缓冲输入流对象中的方法read/readLine读取文本
        /*String line = br.readLine();
        System.out.println(line);

        line = br.readLine();
        System.out.println(line);

        line = br.readLine();
        System.out.println(line);

        line = br.readLine();
        System.out.println(line);*/

        /*
            发下以上读取是一个重复的过程,所以可以使用循环优化
            不知道文件中有多少行数据,所以使用while循环
            while的结束条件,读取到null结束
         */
        String line;
        while((line = br.readLine())!=null){
    
    
            System.out.println(line);
        }

        //3.释放资源
        br.close();
    }
}

5 exercises

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.释放资源
 */
import java.io.*;
import java.util.HashMap;
import java.util.Set;

public class demo05 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        HashMap<String ,String> hash = new HashMap<>();
        BufferedReader br = new BufferedReader(new FileReader("10_IO\\in.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("10_IO\\sorted.txt"));
        String s;
        while ((s=br.readLine())!=null){
    
    
            String[] split = s.split("\\.");
            hash.put(split[0],split[1]);
        }
        Set<String> set = hash.keySet();
        for (String s1 : set) {
    
    
            bw.write(s1+"."+hash.get(s1));
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}



6 byte stream + buffer stream example

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
    文件复制练习:一读一写

    明确:
        数据源: c:\\1.jpg
        数据的目的地: d:\\1.jpg

    文件复制的步骤:
        1.创建一个字节输入流对象,构造方法中绑定要读取的数据源
        2.创建一个字节输出流对象,构造方法中绑定要写入的目的地
        3.使用字节输入流对象中的方法read读取文件
        4.使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
        5.释放资源
    文件的大小:780,831 字节
    一次读写一个字节:6043毫秒
    使用数组缓冲读取多个字节,写入多个字节:10毫秒
 */
public class Demo01CopyFile {
    
    
    public static void main(String[] args) throws IOException {
    
    
        long s = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("c:\\1.jpg");
        FileOutputStream fos = new FileOutputStream("d:\\1.jpg");
        //一次读取一个字节写入一个字节的方式
        /*int len = 0;
        while((len = fis.read())!=-1){
            fos.write(len);
        }*/

        //使用数组缓冲读取多个字节,写入多个字节
        byte[] bytes = new byte[1024];
        int len = 0;//每次读取的有效字节个数
        while((len = fis.read(bytes))!=-1){
    
    
            fos.write(bytes,0,len);
        }

        //5.释放资源(先关写的,后关闭读的;如果写完了,肯定读取完毕了)
        fos.close();
        fis.close();
        long e = System.currentTimeMillis();
        System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
    }

}

7 Character stream + buffer stream example

import java.io.*;
/*
    文件复制练习:一读一写

    明确:
        数据源: c:\\1.jpg
        数据的目的地: d:\\1.jpg
    文件复制的步骤:
        1.创建字节缓冲输入流对象,构造方法中传递字节输入流
        2.创建字节缓冲输出流对象,构造方法中传递字节输出流
        3.使用字节缓冲输入流对象中的方法read,读取文件
        4.使用字节缓冲输出流中的方法write,把读取的数据写入到内部缓冲区中
        5.释放资源(会先把缓冲区中的数据,刷新到文件中)

    文件的大小:780,831 字节
    一次读写一个字节:32毫秒
    使用数组缓冲读取多个字节,写入多个字节:5毫秒
 */
public class Demo02CopyFile {
    
    
    public static void main(String[] args) throws IOException {
    
    
        long s = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:\\1.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\1.jpg"));
        //一次读取一个字节写入一个字节的方式
        /*int len = 0;
        while((len = bis.read())!=-1){
            bos.write(len);
        }*/

        //使用数组缓冲读取多个字节,写入多个字节
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = bis.read(bytes))!=-1){
    
    
            bos.write(bytes,0,len);
        }

        bos.close();
        bis.close();

        long e = System.currentTimeMillis();
        System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
    }
}

Guess you like

Origin blog.csdn.net/TOPic666/article/details/107990563