본문 바로가기

공부/자바

열혈강의 자바 468쪽

반응형

import java.io.*;
import java.util.*;

class Sub implements Serializable{
 private String name;
 private String jumin;
 private String tel;
 private String addr;
 private static transient BufferedReader in;
 static {
  in = new BufferedReader(new InputStreamReader(System.in));
 }
 
 public Sub() throws IOException
 {
  System.out.print("name = ");
  name = in.readLine();
  System.out.print("jumin = ");
  jumin = in.readLine();
  System.out.print("tel = ");
  tel = in.readLine();
  System.out.print("addr = ");
  addr = in.readLine();
 }
 public void disp()
 {
  System.out.print(name + "\t");
  System.out.print(jumin + "\t");
  System.out.print(tel + "\t");
  System.out.println(addr);
 }
}

public class Mains
{
 public static void main(String[] args) throws ClassNotFoundException, IOException
 {
  File dir = new File("c:\\java");
  File file = new File(dir, "myfriends.dat");
  Vector vc = new Vector();
  if(file.exists()){
   ObjectInputStream ois =
    new ObjectInputStream(
      new BufferedInputStream(new FileInputStream(file)));
   vc = (Vector) ois.readObject();
   ois.close();
  }
  while(true){
   System.out.print("1.친구등록 2.전체보기 3.종료 = ");
   int x = System.in.read() -48;
   System.in.read();
   System.in.read();
   if(x == 1){
    Sub ps = new Sub();
    vc.add(ps);
    System.out.println("친구 1명을 등록 하였습니다.");
   } else if(x == 2){
    for(int i=0; i< vc.size(); i++){
     Sub imsi = (Sub) vc.elementAt(i);
     imsi.disp();
    }
   } else if(x == 3){
    ObjectOutputStream oos =
     new ObjectOutputStream(
       new BufferedOutputStream(new FileOutputStream(file)));
    oos.writeObject(vc);
    oos.close();
    System.exit(0);
   } else{
    System.out.println("잘못 입력!!");
   }
  }
 }
}

반응형