JAVA single row diary -2020 / 1 / 27- buffered stream

1. The buffer-flow principle

  • Transport stream is added to a basic buffer (array), so that the byte / character is no longer a one transmission, but the "package" in the array, a one-time transmission,efficient
    Here Insert Picture Description
    Here Insert Picture Description

2. byte stream buffer

2.1 byte buffered output streamBufferedOutputStream

  • Steps for usage:
  1. Create a FileOutputStreamclass object constructor to fill the output destination
  2. Create a BufferedOutputStreamclass object constructor to fill newly created FileOutputStreamclass object
  3. Call the write()method, data is written toBuffer
  4. Call flush()method will flush the buffer data to the hard disk
  5. Call the close()method, the release of memory
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo03 {
    public static void main(String[] args) throws IOException {

        FileOutputStream fileOutputStream = new FileOutputStream("G:\\Java\\测试文件夹\\d.txt");
        BufferedOutputStream buffer = new BufferedOutputStream(fileOutputStream);

        byte[] bytes = {97,98,99,100};
        buffer.write(bytes);

        buffer.flush();

        buffer.close();
    }
}

Here Insert Picture Description

2.2 byte input stream bufferBufferedInputStream

  • Steps for usage:
  1. Create a FileInputStreamclass object constructor to fill in the data source
  2. Create a BufferedInputStreamclass object constructor to fill newly created FileInputStreamclass object
  3. Call the read()method, data is written toBuffer
  4. Call the close()method, the release of memory
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Demo04 {
    public static void main(String[] args) throws IOException {
        FileInputStream file = new FileInputStream("G:\\Java\\测试文件夹\\d.txt");
        BufferedInputStream buffer = new BufferedInputStream(file);

        byte[] bytes =new byte[1024];
        int len = 0;
        while ((len=buffer.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }

        buffer.close();
    }
}

Here Insert Picture Description

3. character buffer flow

3.1 Character buffered output streamBufferedWriter

  • Specific methods: 对象.newLine()wrapping, the system need not be considered

  • Steps for usage:

  1. Create a Writerclass object constructor to fill the output destination
  2. Create a BufferedWriterclass object constructor to fill newly created Writerclass object
  3. Call the write()method, data is written toBuffer
  4. Call flush()method will flush the buffer data to the hard disk
  5. Call the close()method, the release of memory
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class Demo06 {
    public static void main(String[] args) throws IOException {
        FileWriter file = new FileWriter("G:\\Java\\测试文件夹\\e.txt");
        BufferedWriter buffer = new BufferedWriter(file);

        char[] chars = {'a','b','c'};
        buffer.write(chars);
        buffer.newLine();//换行,不需要考虑系统
        buffer.flush();

        char[] chars1 = {'你','好'};
        buffer.write(chars1);
        buffer.flush();
        
        buffer.close();
        
    }
}

Here Insert Picture Description

3.2 buffered input character streamBufferedReader

  • Specific method: 对象.readLine()read a line of text (subject line to line breaks)

  • Steps for usage:

  1. Create a Readerclass object constructor to fill the input source
  2. Create a BufferedReaderclass object constructor to fill newly created Readerclass object
  3. Call reader()/ readLine()method, read all / line data
  4. Call the close()method, the release of memory
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;

public class Demo07 {
    public static void main(String[] args) throws IOException {
        FileReader file = new FileReader("G:\\Java\\测试文件夹\\f.txt");
        BufferedReader buffer = new BufferedReader(file);

        String str = buffer.readLine();
        System.out.println(str);
        
        buffer.close();
    }
}

Here Insert Picture Description
Here Insert Picture Description

Published 103 original articles · won praise 1 · views 2621

Guess you like

Origin blog.csdn.net/wangzilong1995/article/details/104095567