Advanced IO operations in Java

Advanced IO operations in Java

1. Serialization and deserialization

It is often used to transfer data of Java objects between multiple servers.

1. Concept

Serialization

It refers to the Java objects prepared in the program and permanently saved on the disk. This process is actually an out action.

  • ObjectOutputStream: Persistent storage of objects can be achieved by using files in streams.
  • Create object: ObjectOutputStream(OutputStream out)
  • Commonly used methods:
    void writeObject(Object obj): Write the specified object to ObjectOutputStream
Deserialization

It refers to the action of reading the serialized files from the disk into the program and restoring them into Java objects. It is an in process.

  • ObjectInputStream: Deserialize basic data and objects previously written using ObjectOutputStream.
  • Create object: ObjectInputStream(InputStream in)
  • Commonly used methods:
    Object readObject(): Read objects from ObjectInputStream.

2. Features/application scenarios

  1. Files that need to be serialized must implement the Serializable interface to enable its serialization function;
  2. Data that does not need to be serialized can be modified as static. Since static belongs to a class, it will not be serialized and output along with the object;
  3. Data that does not need to be serialized can also be modified as transient. It only exists in the memory during the running of the program and will not be serialized and persisted;
  4. During deserialization, if it is inconsistent with the serialized version number, deserialization cannot be completed;
    5. Each serialized file has a unique id. If it is not added, the compiler will calculate it based on the class definition information. Generate a version number;
  5. Commonly used for data transmission between servers, serialized into files, and deserialized to read data;
  6. Commonly used to pass objects between hosts using socket streams.
package cn.tonyoliver.io;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 这个类用来测试序列化和反序列化
 * 多态的好处:统一调用标准,父类是标准。
 * @author Tony
 *
 */
public class Test04_Seriaizable {
    
    
	public static void main(String[] args) throws Exception {
    
    
		seria();// 序列化
		deseria();// 反序列化
	}

	// 序列化
	private static void seria() {
    
    
		try {
    
    
			// 1,创建对象
			// 此时不适合使用多态,因为InputStream没有writeObject()方法
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Z:\\iotest\\1.txt"));
			// 2,完成序列化
			Student s = new Student("Mojito", 20);
			out.writeObject(s);
			// 3,释放资源
			out.close();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		}

	}

	// 反序列化
	private static void deseria() throws Exception {
    
    
		// 1,创建对象
		// 此时不适合使用多态,因为InputStream没有readObject()方法
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("Z:\\iotest\\1.txt"));
		// 2,完成反序列化
		Object obj = in.readObject();
		System.out.println(obj);
		// 3,释放资源
		in.close();
	}
}

2. Encoding conversion stream

You can only convert a byte stream into a character stream, not a character stream into a byte stream.

Used as a bridge to convert byte streams into character streams. Used to solve the problem of garbled characters written in the character stream.

1. Tools

InputStreamReader
  • InputStreamReader is a bridge from a byte stream to a character stream: it reads bytes using the specified charsetand decodes them into characters. The character set it uses can be specified by name or given explicitly, or it can accept the platform's default character set.
Construction method
  • InputStreamReader(InputStream in)
    creates an InputStreamReader that uses the default character set.
  • InputStreamReader(InputStream in, String charsetName)
    creates an InputStreamReader using the specified character set.
OutputStreamReader
  • OutputStreamWriter is a bridge from byte stream to character stream: you can use the specified charsetcharacters to be written to the stream to encode them into bytes. The character set it uses can be specified by name or given explicitly, otherwise the platform default character set will be accepted.
Construction method
  • OutputStreamWriter(OutputStream out)
    creates an OutputStreamWriter using the default character encoding.
  • OutputStreamWriter(OutputStream out, String charsetName)
    creates an OutputStreamWriter using the specified character set.
package cn.tonyoliver.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

/**
 * 这个类用来测试编码转换流
 * 
 * @author Tony
 *
 */
public class Test05_Encoding {
    
    
	public static void main(String[] args) throws IOException {
    
    
		out();// 测试OutputStreamWriter
		in();// 测试InputStreamReader
	}

	// OutputStreamWriter
	private static void out() throws IOException {
    
    
		// 1,创建对象
		Writer out = new OutputStreamWriter(new FileOutputStream("Z:\\iotest\\1.txt"));
		// 2,写出数据
		out.write(97);
		out.write(98);
		out.write("大家好~");
		// 3,释放资源
		out.close();
	}

	// InputStreamReader
	private static void in() throws IOException {
    
    
		// 1,创建对象
		Reader in = new InputStreamReader(new FileInputStream("Z:\\iotest\\1.txt"));
		// 2,读取数据
		int b = 0;
		while ((b = in.read()) != -1) {
    
    
			System.out.println(b);
		}
		// 3,释放资源
		in.close();
	}
}

Guess you like

Origin blog.csdn.net/weixin_45015214/article/details/109245157