Java manipulates IO object stream to read and write data

  • 2021-10-15 10:41:03
  • OfStack

Read and write objects
Use ObjectInputStream and ObjectOutputStream to read and write objects (serialization and deserialization).

Only byte stream no character stream

Class must implement the Serializable interface Add a serialization number to the class, define a tag to the class, and the new modified class can also operate on the objects that have been serialized Static can't be serialized. Serialization can only be serialized in the heap, not in the "method area" Prefix fields that do not need serialization with transient

Small examples:

First create an Dog object and serialize it:


package com.uwo9.test03;
 
import java.io.Serializable;
 
public class Dog implements Serializable {
	private static final long serialVersionUID = 2809685095868158625L;
	String name;
	String color;
}

Create another Student object and serialize it:


package com.uwo9.test03;
 
import java.io.Serializable;
 
public class Student implements Serializable {
	private static final long serialVersionUID = 9078616504949971001L;
	static public  String schoolName;
	private transient String name;
	private transient int age;
	private double score;
	private Dog dog;
	public Student() {
		super();
	}
	public Student(String name, int age, double score, Dog dog) {
		super();
		this.name = name;
		this.age = age;
		this.score = score;
		this.dog = dog;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
	}
 
}

Write data to an object stream and save it to a file


package com.uwo9.test03;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
 
public class Test01 {
 
	public static void main(String[] args) {
		Dog dog = new Dog();
		dog.name = " Rhubarb ";
		dog.color = "Yellow";
		Student student1 = new Student(" Students 1", 18, 99,dog);
		Student student2 = new Student(" Students 2", 19, 99,dog);
		Student student3 = new Student(" Students 3", 20, 99,dog);
		Student.schoolName = " So-and-so university ";
		File file = new File("E:/Temp/Test1.txt");
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(file));
			//oos.writeObject(student);
			ArrayList<Student> arrayList = new ArrayList<>();
			Collections.addAll(arrayList, student1,student2,student3);
			oos.writeObject(arrayList);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
 
}

Reads an object from a specified file


package com.uwo9.test03;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
 
public class Test02 {
 
	public static void main(String[] args) {
		//  Reads an object from the specified file 
		File file = new File("E:/Temp/Test1.txt");
		ObjectInputStream ois=null;
		try {
			ois = new ObjectInputStream(new FileInputStream(file));
			//  Read object 
			// Student stu = (Student)ois.readObject();
			// System.out.println(" The read data is :"+stu);
			@SuppressWarnings("unchecked")
			ArrayList<Student> arrayList = (ArrayList<Student>) ois.readObject();
			for (Student student : arrayList) {
				System.out.println(student);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally {
			try {
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
 
	}
 
}

Related articles: