import java.io.Serializable;
import static java.lang.System.out;
// ObjectInputStream / ObjectOutputStream 예제
public class Employee implements Serializable //객체를 저장하기위해 반드시 구현
{
String name;
int age;
transient int salary;
public Employee() {
}
public Employee(String n, int a, int s) {
name=n; age=a; salary=s;
}
public void print() {
out.println("---------Record for" +name+"--------");
out.println(" Name: " +name);
out.println(" Age: " +age);
out.println(" : " +salary);
}
}////////////////////////////////////////////////////////////////////
import java.io.*;
import java.awt.*;
import java.util.*;
// 기능 : 객체를 파일로 저장
class SaveEmp
{
public static void main(String[] args) throws IOException
{
Employee e1=new Employee("홍길동", 22, 3000);
Employee e2=new Employee("임꺽정", 33, 4000);
String fileName="obj.ser";
FileOutputStream fos =new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
//DataOutput인터페이스를 구현하고 있는 스트림.
oos.writeObject(e1);
oos.writeObject(e2);
oos.writeObject(new Date());
Frame f=new Frame("오브젝트 스트림");
oos.writeObject(f);
oos.flush();
oos.close();
fos.close();
}//main()-----------
}//////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.awt.*;
import java.util.*;
// 기능 : 객체를 파일로 저장
class ReadEmp
{
public static void main(String[] args) throws Exception
{
FileInputStream fis=new FileInputStream("obj.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Object o1=ois.readObject();
Employee e1=(Employee)o1;
Employee e2=(Employee)ois.readObject();
Date d=(Date)ois.readObject();
Frame f=(Frame)ois.readObject();
e1.print();
e2.print();
System.out.println("저장된 객체 시간:"+d.toString());
System.out.println("현재 시간:"+new Date().toString());
f.setSize(200,200);
f.setVisible(true);
fis.close();
ois.close();
}
}