104105 Buffer Stream Based on Java

Keywords: Java

Buffer flow

First of all, we should clarify a concept:
Frequent read and write operations on files or other targets are inefficient and poor in performance.
The advantage of using buffer stream is that it can read and write information more efficiently. The principle is to buffer data first, then write or read it together.
It is divided into byte buffer stream and character buffer stream.
BufferedInputStream: Adding some functionality to another input stream creates an internal buffer array for buffering data when creating BufferedInputStream.
Buffered Output Stream: By setting this output stream, the application can write bytes to the underlying output stream without calling the underlying system for each byte write.
BufferedReader: Read text from character input stream, buffer each character, so as to achieve efficient reading of characters, arrays and rows.
BufferedWriter: Writes text into character output streams, buffering individual characters, thus providing efficient writing of individual characters, arrays, and strings.

package com.vince;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

/**
 * The purpose of caching is:
 * To solve the problem of performance degradation caused by frequent operation of files when writing files.
 * BufferedOutputStream The default internal cache size is 8KB, which is stored in the byte array of the cache each time it is written. When the array is full, the data in the array is written to the file.
 * And cache subscripts to zero
 * 
 * Character stream
 * 1,Add character buffer stream to enhance readLine
 * 2,More efficient reading of data
 * FileReader: Input Stream Reader (sun. nio. cs. Stream Decoder) is used internally. The decoding process is byte - > char. The default cache size is 8K.
 * BufferedReader:The default cache size is 8K, but you can specify the cache size manually, read data into the cache, reduce each conversion process, and be more efficient.
 * BufferedWriter Ditto
 * 
 * @author vince
 * @description
 */
public class BufferStreamDemo {

	private static void charWriter(){
		File file = new File("c://test//vince.txt");
		try {
			Writer writer = new FileWriter(file);
			BufferedWriter bw = new BufferedWriter(writer);
			bw.write("The village flowers come to my house");
			bw.flush();
			bw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void charReader(){
		File file = new File("c://test//vince.txt");
		try {
			Reader reader = new FileReader(file);
			//Provide caching for character streams to achieve efficient reading
			BufferedReader br = new BufferedReader(reader);
			char[] cs = new char[1024];
			int len = -1;
			while((len=br.read(cs))!=-1){
				System.out.println(new String(cs,0,len));
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void byteReader2(){
		File file = new File("c://test//vince.txt");
		try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
//The new writing after 1.7 is placed in try() parentheses so that you don't have to manually close try to help automatically close it.
			byte[] bytes = new byte[1024];
			int len = -1;
			while((len=bis.read(bytes))!=-1){
				System.out.println(new String(bytes,0,len));
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private static void byteReader(){
		File file = new File("c://test//vince.txt");
		try {
			InputStream in = new FileInputStream(file);
			//Construct a byte buffer stream
			BufferedInputStream bis = new BufferedInputStream(in);
			
			byte[] bytes = new byte[1024];
			int len = -1;
			while((len=bis.read(bytes))!=-1){
				System.out.println(new String(bytes,0,len));
			}
			bis.close();			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private static void byteWriter(){
		File file = new File("c://test//vince.txt");
		try {
			OutputStream out = new FileOutputStream(file);
			//Construct a byte buffer stream
			BufferedOutputStream bos = new BufferedOutputStream(out);
			
			//
			String info = "The river is still running.";
			bos.write(info.getBytes());
			
			bos.close();
			//out.close(); // There is no need to close the bos.close() method to help close out


			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
//		byteWriter();
		byteReader();
		
	}

}

Posted by someone2088 on Sat, 20 Apr 2019 14:51:33 -0700