studentdemo.java

来自「java课程的资料以及实验的代码」· Java 代码 · 共 56 行

JAVA
56
字号
import java.io.*;
class Student implements Serializable
{
	int id;
	String name;
	int age;
	String intro;
	
	public Student(int id,String name,int age,String intro)
	{
		this.id=id;
		this.name=name;
		this.age=age;
		this.intro=intro;
	}
	public String toString()
	{ 
		return "id=" + id + ";name=" + name + ";age=" + age+ ";intro=" + intro; 
	} 
}
public class StudentDemo
{
  
	public static void main(String args[]) throws IOException, ClassNotFoundException
	{
		Student stu=new Student(981036,"Liu Ming",18,"CSD");
		System.out.println("Student:"+stu.toString());
		FileOutputStream fo=new FileOutputStream("data.txt");
		//keep the Object status;
		ObjectOutputStream so=new ObjectOutputStream(fo);
		try
		{
			so.writeObject(stu);
			so.flush();
			so.close();
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		FileInputStream fi=new FileInputStream("data.txt");
		ObjectInputStream si=new ObjectInputStream(fi);
		
		try
		{
			stu=(Student)si.readObject();
			System.out.println("Student:"+stu.toString());
			si.close();
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?