Detailed explanation: IO stream in java

IO style

I: stands for Input, O: stands for Output. For input and output to files, Java programs complete input/output through streams, and all input/output is processed in the form of streams. Therefore, to understand the I/O system, we must first understand the concept of input/output streams.
Input is to read data from various input devices (including files, keyboards, etc.) into memory, and output is just the opposite, which is to write data to various output devices (such as files, displays, disks, etc.). For example, a keyboard is a standard input device, and a monitor is a standard output device, but a file can be used as both an input device and an output device.
1. A data stream is an object that Java performs I/O operations, and it can be divided into different categories according to different standards.
2. According to the direction of the flow, it is mainly divided into two categories: input flow and output flow.
3. The data stream is divided into byte stream and character stream according to the different data units.
4. According to the function, it can be divided into node flow and processing flow.

input stream

InputStream byte stream As a parent class of the input stream, it is an abstract class and cannot instantiate objects, so the important things are left to its own subclasses to complete.
insert image description here
The commonly used methods are:
insert image description here
the last three methods above are generally used together. First, use markSupported() to judge. If it can be read repeatedly, use the mark(int readLimit) method to mark it. After the mark is completed, you can use the read() method. Read the number of bytes in the marked range, and finally use the reset() method to relocate the input stream to the marked position, and then complete the repeated read operation.
Characters in Java are Unicode encoding, that is, double-byte, and InputerStream is used to process single-byte, which is not very convenient when dealing with character text. At this time, you can use Java's text input stream Reader class, which is an abstract class of character input stream, that is, the implementation of all character input streams is its subclass, and the methods of this class are similar to those of the InputerSteam class.
Reader character stream
is mainly used for reading txt documents and strings. It can directly process double bytes, which is faster than single-byte reading. Of course the byte stream can be converted to a character stream.
insert image description here
method:
insert image description here

output stream

OutputStream byte stream
All output stream classes in Java are subclasses of the OutputStream abstract class (byte output stream) and the Writer abstract class (character output stream). The OutputStream class is an abstract class of byte output streams, and is the parent class of all byte output streams.
insert image description here
Commonly used methods:
insert image description here
Writer character
stream An abstract class for writing character streams. The only methods that a subclass must implement are write(char[], int, int), flush() and close(). However, most subclasses will override some of the methods defined here to provide greater efficiency and/or additional functionality.
insert image description here
Common method:
insert image description here

Conversion between byte stream and character stream

It can be found from the InputStreamReader class of the character stream that it inherits from the Reader class, and the parameters in the construction method can be InputStream stream objects. Of course, InputStreamReader itself is also a character input stream, but BufferReader is also required at this time (writing text into the character output stream, Buffers individual characters, providing efficient writing of individual characters, arrays, and strings.) The buffered character stream wraps around so Reader read = new BufferReader(new InputStreamReader(new FileInputStream(new File(""))))the whole socket ends up.

package com.openlab.coll;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class InputSteamReaderTest {
    
    
	
	public static void main(String[] args) throws IOException {
    
    
		//读取的目标
		String path="D:\\2.txt";
		//写入目标路径
		File target = new File("mydir1/mydir2/a.txt");
		
//		FileInputStream fis = new FileInputStream(path);
//		
//		InputStreamReader isr = new InputStreamReader(fis);
//		
//		BufferedReader br = new BufferedReader(isr);
		
		BufferedReader br = 
				new BufferedReader(
						new InputStreamReader(
								new FileInputStream(path)));
			
		
		BufferedWriter bw = 
				new BufferedWriter(
						new OutputStreamWriter(
								new FileOutputStream(target)));
		
		
		String result = "";
		
		StringBuffer sb = new StringBuffer();
		
		while((result = br.readLine())!=null){
    
    
			// 写的操作
			
			  bw.write(result);
			  bw.newLine();
			  sb.append(result+"\n");
			
		}
		
		System.out.println(sb.toString());
		
		bw.flush();
		br.close();
		bw.close();
		
	}

}

Serialize and deserialize

Interface: Serializable is only used for serialization identification. There are no methods and no fields in this interface. Serialization
and deserialization:
Whether the object we define can be transmitted or processed in the stream
DataInputStream The basic data type encapsulation class
Byte Boolean Double Char Integer Short Float Long
serializes the reference data type - read and write the reference data type through the stream
ObjectInputStream The parent class is InputStream which implements the DataInput interface
ObjectOutputStream The parent class OutputSream implements the DataOutput interface
Precondition: The
reference data type must be implemented Serializable interface Serialization
and deserialization process objects, that is, binary data streams are stored
. Objects are stored in serialized form in txt ----- text ----- characters
and serialized objects are bytes Displaying the original byte object in the form of a string in the txt text will naturally cause garbled characters. After opening the txt text with sublimeText, there is hexadecimal data inside.

package com.openlab.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class ObjectInputStreamTest {
    
    
	
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    
    
		
		File file = new File("mydir1/mydir2/a.txt");
		Person p = new Person("张三疯",99);
		
		// 需要一个InputStream 对象
		// 将对象序列化
		ObjectOutputStream oos = 
				new ObjectOutputStream(new FileOutputStream(file));
		
		// 做写的操作
		// 把对象写入到文档中后再进行去取
		oos.writeObject(p);
		
		
		//将对象反序列化
		ObjectInputStream ois = 
				new ObjectInputStream(new FileInputStream(file) );
		
		Person p1 = (Person) ois.readObject();
		
		System.out.println(p1.getName());
		System.out.println(p1.getAge());
		
		oos.flush();
		ois.close();
		oos.close();
		
	}

}

Guess you like

Origin blog.csdn.net/cout_s/article/details/119978869