Detailed Explanation of Character Buffer Stream Instance of Java IO Stream

  • 2021-07-26 07:39:27
  • OfStack

Character stream:

1. Add character cache stream to enhance reading function (readLine)

2. Read data more efficiently

BufferedReader

Read text from the character input stream and buffer each character, thus realizing efficient reading of characters, arrays and lines.

FileReader: Internal use of InputStreamReader, decoding process, byte- > char, default cache size is 8k

BufferReader: The default cache size is 8k, but you can manually specify the cache size to read data into the cache, reducing each conversion process and making it more efficient


/ Character input buffer stream 
	private static void charReader() {
		// Object file 
		File file = new File("F:\\javatest\\lemon1.txt");
		try {
			// Character stream 
			Reader reader = new FileReader(file);
			// It provides buffer for character stream, which has achieved the purpose of efficient reading 
			BufferedReader bufr = new BufferedReader(reader);
			
			char[] chars = new char[1024];
			int len = -1;
			while((len = bufr.read(chars)) != -1) {
				System.out.println(new String(chars,0,len));
			}
      bufr.close();
				
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

BufferedWriter

Writes text to the character output stream, buffering individual characters to provide efficient writing of individual characters, arrays, and strings

FileWriter: Internal use of InputStreamWriter, decoding process, byte- > char, default cache size is 8k

BufferWriter: The default cache size is 8k, but you can manually specify the cache size to read data into the cache, reducing each conversion process and making it more efficient


// Character output buffer stream 
	private static void charWriter() {
		// Object file 
		File file = new File("F:\\javatest\\lemon1.txt");
		try {
			// Character stream 
			Writer writer = new FileWriter(file,true);// Append 
			// It provides buffer for character stream, which has achieved the purpose of efficient reading 
			BufferedWriter bufr = new BufferedWriter(writer);
			bufr.write(" Here is the character buffer stream \r\n");
			bufr.flush();
			bufr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

Summary:


package com.lemon;
 
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;
/**
 *  Purpose of caching: 
 *  Solve the problem of performance degradation caused by frequent operation of files when writing files 
 * BufferedOutputStream The internal default cache size is 8kb That is stored in the cache every time it is written byte Array, when the array is full, the data in the array will be written to the file 
 *  And the cache subscript returns to zero  
 * 
 *  Character stream: 
 * 1 Add character cache stream and enhance read function ( readLine ) 
 * 2 Read data more efficiently 
 * FileReader : Internal use InputStreamReader , decoding process, byte->char The default cache size is 8k
 * BufferReader The default cache size is 8k However, you can manually specify the cache size to read data into the cache, reducing each conversion process and making it more efficient 
 * BufferedWriter Ibid. 
 * @author lemonSun
 *
 * 2019 Year 5 Month 4 Sunday afternoon 8:12:53
 */
public class BufferStreamDemo {
 
	public static void main(String[] args) {
	//	byteWriter();
	//	byteReader();
//		byteReader1();
//		charReader();
		charWriter();
	}
	
	// Character output buffer stream 
	private static void charWriter() {
		// Object file 
		File file = new File("F:\\javatest\\lemon1.txt");
		try {
			// Character stream 
			Writer writer = new FileWriter(file,true);// Append 
			// It provides buffer for character stream, which has achieved the purpose of efficient reading 
			BufferedWriter bufr = new BufferedWriter(writer);
			bufr.write(" Here is the character buffer stream \r\n");
			bufr.flush();
			bufr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	// Character input cache stream 
	private static void charReader() {
		// Object file 
		File file = new File("F:\\javatest\\lemon1.txt");
		try {
			// Character stream 
			Reader reader = new FileReader(file);
			// It provides buffer for character stream, which has achieved the purpose of efficient reading 
			BufferedReader bufr = new BufferedReader(reader);
			
			char[] chars = new char[1024];
			int len = -1;
			while((len = bufr.read(chars)) != -1) {
				System.out.println(new String(chars,0,len));
			}
			bufr.close();
				
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	// Cache stream input   No need to close  try Automatic shutdown   Must be realized Closeable Interface 
	private static void byteReader1(){
		// Object file 
		File file = new File("F:\\javatest\\lemon1.txt");
		
		//buf Scope in try Multiple statements in braces try(;) , ; Separate  
		try(BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file))) {	
			byte[] bytes = new byte[1024];
			int len = -1;
			while((len = buf.read(bytes)) != -1) {
				System.out.println(new String(bytes,0,len));
			}
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	// Cache stream input 
	private static void byteReader(){
		// Object file 
		File file = new File("F:\\javatest\\lemon1.txt");
		
		try {
			// Byte output stream 
			InputStream in = new FileInputStream(file);
			// Byte buffer stream 
			BufferedInputStream buf = new BufferedInputStream(in);
			byte[] bytes = new byte[1024];
			int len = -1;
			while((len = buf.read(bytes)) != -1) {
				System.out.println(new String(bytes,0,len));
			}
			buf.close();// Automatic shutdown  in.close
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	// Cache stream output 
	private static void byteWriter(){
		// Object file 
		File file = new File("F:\\javatest\\lemon1.txt");
		
		try {
			// Byte output stream 
			OutputStream out = new FileOutputStream(file,true);
			// Buffer flow 
			BufferedOutputStream buf = new BufferedOutputStream(out);
			// Content 
			String info = " Here is the buffered stream \r\n";
			// Write 
			buf.write(info.getBytes());
			buf.close(); //jdk1.7 Automatically shut down later  out
		//	out.close();
				
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
 
}

Related articles: