Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
970 B
1
Indexable
Never
import java.io.Serializable;

@SuppressWarnings("serial")
public class Student implements Serializable{
	int id;
	String name;
	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	}
}

import java.io.*;

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();
		}
		catch (Exception e){
			System.out.print(e);
		}
	}
}


import java.io.*;
public class Depersist {
	public static void main(String[] args) {
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream("f.txt"));
			Student s = (Student)in.readObject();
			System.out.println("RollNo: " + s.id + "\nName: " + s.name);
			in.close();
		}
		catch (Exception e){
			System.out.println(e);
		}
	}
}