Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
982 B
9
Indexable
import java.io.Serializable;
public class Student implements Serializable {
	int id;
	String name;
	public Student(int id,String name) {
		this.id=id;
		this.name=name;
	}
}

import java.io.*;
public class Persist {
	public static void main(String[] args) {
		try {
			Student s1 =new Student(211,"ravi");
			
			FileOutputStream fout= new FileOutputStream("f.txt");
			ObjectOutputStream out = new ObjectOutputStream(fout);
			out.writeObject(s1);
			out.flush();
			out.close();
			System.out.println("Success");
		}catch(Exception e) {
			System.out.println(e);
		}
	}
}

import java.io.*;
public class Depersist {

	public static void main(String[] args) {
		try {
			FileInputStream fin = new FileInputStream("f.txt");
			ObjectInputStream in = new ObjectInputStream(fin);
			Student s = (Student)in.readObject();
			in.close();
			System.out.println(s.name + " " + s.id);
		}catch(Exception e) {
			System.out.println(e);
		}
	}

}