Io conversion stream and buffer stream of java basic syntax

Posted by Fearpig on Wed, 29 Dec 2021 00:48:24 +0100

catalogue

Conversion flow

OutputStreamWriter class

InputStreamReader class

The difference between transformation flow and subclass

Buffer stream

Byte buffer stream

Byte BufferedOutputStream

Byte buffered input stream BufferdInputStream

Character buffer stream

Character buffered output stream BufferedWriter

Character buffered input stream BufferedReader

Conversion flow

OutputStreamWriter class

The function is to convert the string into bytes according to the specified encoding table, and write out these bytes using the byte stream

Case

public static void writeCN() throws Exception {
		//Creates a byte output stream object associated with a file
		FileOutputStream fos = new FileOutputStream("c:\\cn8.txt");
		//Create a conversion stream object that can convert characters to bytes and specify the encoding
		OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
		//Call the conversion stream and write the text, which is actually written to the buffer of the conversion stream
		osw.write("Hello");//Write buffer.
		osw.close();
	}

Principle: OutputStreamWriter stream object, stored in its cache area, converted the character encoding value into byte storage, called refresh function, or closed the flow, or the buffer byte was stored, then the byte data in the buffer was written to the specified file using byte stream.

InputStreamReader class

InputStreamReader is a bridge between byte flow and character flow: it uses the specified character encoding table to read bytes and decode them into characters

Case

public class InputStreamReaderDemo {
	public static void main(String[] args) throws IOException {
		//Demonstrates the conversion of byte to character stream
		readCN();
	}
	public static void readCN() throws IOException{
		//Creates a byte stream object that reads files
		InputStream in = new FileInputStream("c:\\cn8.txt");
		//Create a transformation flow object 
		//InputStreamReader isr = new InputStreamReader(in); When an object is created in this way, it will be read with the local default code table, and an error decoding error will occur
        InputStreamReader isr = new InputStreamReader(in,"utf-8");
		//Use the conversion stream to read the bytes in the byte stream
		int ch = 0;
		while((ch = isr.read())!=-1){
			System.out.println((char)ch);
		}
		//Close flow
		isr.close();
	}
}

Note: when reading a file with a specified encoding, be sure to specify the encoding format, otherwise decoding errors will occur and garbled code will occur

The difference between transformation flow and subclass

There is an inheritance relationship

The following inheritance relationships are found:

OutputStreamWriter:

                  |--FileWriter:

InputStreamReader:

|--FileReader;

What is the difference between the functions of parent and child classes?

OutputStreamWriter and InputStreamReader are bridges between characters and bytes: they can also be called character conversion streams. Principle of character conversion stream: byte stream + encoding table.

FileWriter and FileReader: as subclasses, they exist only as convenient classes for operating character files. When the character file of the operation uses the default encoding table, the operation can be completed directly with Subclasses instead of parent classes, which simplifies the code.

InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));// Default character set.

InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"GBK");// Specifies the GBK character set.

FileReader fr = new FileReader("a.txt");

The functions of these three sentences are the same, and the third sentence is the most convenient.

Note: once you want to specify another encoding, you must not use subclasses. You must use character conversion stream. When to use subclasses?

Conditions:

1. The operation is a file. 2. Use default encoding.

Summary:

Byte -- > character: can't understand -- > can understand. Need to read. Input stream. InputStreamReader

Character -- > byte: understood -- > not understood. Need to write. Output stream. OutputStreamWriter

Buffer stream

The purpose is to improve the read and write speed of io streams

Classify byte buffer stream and character buffer stream

Byte buffer stream

  1. Write data to the stream, byte buffer output stream BufferedOutputStream
  2. Read the data in the stream, byte buffer input stream BufferedInputStream

Byte BufferedOutputStream

Read and write files through byte buffer

Construction method

public BufferedOutputStream (OutputStream out) creates a buffer stream to write data to the underlying input stream

case

public class BufferedOutputStreamDemo01 {
	public static void main(String[] args) throws IOException {
		
		//Method of writing data to file
		write();
	}

	/*
	 * Method of writing data to file
	 * 1,Create stream
	 * 2,Write data
	 * 3,Close flow
	 */
	private static void write() throws IOException {
		//Create a basic byte output stream
		FileOutputStream fileOut = new FileOutputStream("abc.txt");
		//Use efficient flow to encapsulate the basic flow to improve the speed
		BufferedOutputStream out = new BufferedOutputStream(fileOut);
		//2. Write data
		out.write("hello".getBytes());
		//3. Close the flow
		out.close();
	}
}

Byte buffered input stream BufferdInputStream

The operation of reading data from a file

Construction method

public BufferdInputStream(InputStream in)

case

/*
	 * Read data from file
	 * 1,Create buffer stream object
	 * 2,Read data, print
	 * 3,close
	 */
	private static void read() throws IOException {
		//1. Create a buffer stream object
		FileInputStream fileIn = new FileInputStream("abc.txt");
		//Packaging basic streams into efficient streams
		BufferedInputStream in = new BufferedInputStream(fileIn);
		//2. Read data
		int ch = -1;
		while ( (ch = in.read()) != -1 ) {
			//Print
			System.out.print((char)ch);
		}
		//3. Close
		in.close();
	}

Character buffer stream

Character buffered input stream BufferedReader

Character buffer output stream BufferWriter

Complete the efficient writing and reading of text data

Character buffered output stream BufferedWriter

Purpose to write text into character output stream and buffer each character, so as to provide efficient writing of single character, array and string

New line feed

void newLine() writes a newline character according to the current system

/*
 * BufferedWriter Character buffered output stream
 * method
 * 	public void newLine()Write a line separator
 * 
 * Requirement: write data to file through buffered output stream
 * analysis:
 * 	1,Create flow object
 * 	2,Write data
 * 	3,Close flow
 * 
 */
public class BufferedWriterDemo {
	public static void main(String[] args) throws IOException {
		//Create stream
		//Basic character output stream
		FileWriter fileOut = new FileWriter("file.txt");
		//Wrap the basic flow
		BufferedWriter out = new BufferedWriter(fileOut);
		//2. Write data
		for (int i=0; i<5; i++) {
			out.write("hello");
			out.newLine();
		}
		//3. Close the flow
		out.close();
	}
}

Character buffered input stream BufferedReader

Aim to read text from character input stream and buffer each character, so as to realize efficient reading of characters, arrays and lines.

The new method is public String readLine(); Read one line at a time and return null at the end of the stream

case

/*
 * BufferedReader Character buffered input stream
 * 
 * method:
 * 	String readLine() 
 * Requirements: read data from the file and display the data
 */
public class BufferedReaderDemo {
	public static void main(String[] args) throws IOException {
		//1. Create flow
		BufferedReader in = new BufferedReader(new FileReader("file.txt"));
		//2. Read data
		//One character at a time
		//One character array at a time
		//Read the string content of one line in the text at a time
		String line = null;
		while( (line = in.readLine()) != null ){
			System.out.println(line);
		}
		
		//3. Close the flow
		in.close();
	}
}

Topics: Java