package com.serial; import java.io.Serializable; //Serializable 인터페이스를 구현하는 것은 해당클래스가 직렬화 대상임을 명시하는 역할 public class Customer implements Serializable{ //멤버 변수 private String name; //생성자 public Customer(String name){ this.name=name; } public void setName(String name){ this.name=name; } public String getName(){ return name; } //Object to String()재정의 public String toString(){ return "당신의 이름: "+name; } public static void main(String[] args) { } }
package com.serial; import java.io.ObjectOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.FileInputStream; public class ObjectInOutputStreamEx { public static void main(String[] args) { ObjectOutputStream oos=null; FileOutputStream fos=null; ObjectInputStream ois=null; FileInputStream fis=null; try{ fos=new FileOutputStream("Object.ser"); //객체 직렬화 oos=new ObjectOutputStream(fos); oos.writeObject(new Customer("홍길동")); fis=new FileInputStream("object.ser"); ois=new ObjectInputStream(fis); Customer m=(Customer)ois.readObject(); System.out.println(m); }catch(IOException ioe){ ioe.printStackTrace(); }catch (ClassNotFoundException cnfe){ cnfe.printStackTrace(); }finally{ try{ if(oos!=null)oos.close(); if(fos!=null)fos.close(); if(ois!=null)ois.close(); if(fis!=null)fis.close(); }catch(IOException ioe){ ioe.printStackTrace(); } } } }
package com.serial; import java.io.Serializable; public class UserInfo implements Serializable{ String id; String password; int num; public UserInfo(String id, String password,int num){ this.id= id; this.password= password; this.num= num; } @Override public String toString(){ return "UserInfo {id="+id+", password ="+ password+", num="+num+"}"; } }
package com.serial; import java.util.ArrayList; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.IOException; public class SerialEx1 { public static void main(String[] args) { try{ String fileName = "UserInfo.ser"; FileOutputStream fos = new FileOutputStream(fileName); ObjectOutputStream out = new ObjectOutputStream(fos); UserInfo u1 =new UserInfo("JavaMan","1234",30); UserInfo u2 =new UserInfo("JavaWoMan","4321",26); ArrayList list = new ArrayList(); list.add(u1); list.add(u2); //객체를 직렬화 한다. out.writeObject(u1); out.writeObject(u2); out.writeObject(list); out.close(); fos.close(); System.out.println("직렬화가 잘 끝났습니다."); }catch(IOException e){ e.printStackTrace(); } }//main }//class
package com.serial; import java.util.ArrayList; import java.io.ObjectInputStream; import java.io.FileInputStream; import java.io.IOException; public class SerialEx2 { public static void main(String[] args) { try{ String fileName = "UserInfo.ser"; FileInputStream fis = new FileInputStream(fileName); ObjectInputStream in = new ObjectInputStream(fis); //객체를 읽을때는 출력한 순서와 일치해야 한다. UserInfo u1 = (UserInfo)in.readObject(); UserInfo u2 = (UserInfo)in.readObject(); ArrayList list = (ArrayList)in.readObject(); System.out.println(u1); System.out.println(u2); System.out.println(list); in.close(); fis.close(); }catch(Exception e){ e.printStackTrace(); } }//main }//class
'Java > 입출력' 카테고리의 다른 글
예비 (0) | 2012.04.13 |
---|---|
File 생성 및 변경 (0) | 2012.04.13 |
FileEx4 (0) | 2012.04.13 |
디렉토리 정보 (0) | 2012.04.13 |
입출력으로 파일 상세정보 (0) | 2012.04.13 |