ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • FileReader & BufferedReader & ObjectOutputStream & ObjectInputStream
    개발노하우/Java 2007. 5. 4. 10:14

    // 파일을 입력받아 그 내용을 출력
    import java.io.*;

    public class object {
     public static void main(String arg[])throws IOException{
     
      File f = new File("ccc.txt");
      FileReader fr = new FileReader(f);
      BufferedReader br = new BufferedReader(fr);
     
      while(true)
      {
       String s = br.readLine();
       if(s==null) break;
       System.out.println(s);
      }
      br.close();
     }
    }


    ============================================================================
    // 객체 입력
    import java.io.*;

    class AAA implements Serializable{ // 객체 직렬화
     int x = 100;
     int y = 200;
     int z = 300;
    }

    public class object {
     public static void main(String arg[])throws IOException{
      AAA aa = new AAA();
      File f = new File("ddd.txt");
      FileOutputStream fos = new FileOutputStream(f);
      BufferedOutputStream bos = new BufferedOutputStream(fos);
      ObjectOutputStream oos = new ObjectOutputStream(bos);
     
      oos.writeObject(aa);
      oos.close();
     
      System.out.println("출력 완료");
     }
    }

    ============================================================================
    // 객체 출력
    import java.io.*;

    class AAA implements Serializable{ // 객체 직렬화
      int x = 100;
      int y = 200;
      int z = 300;
     }

    public class object {
     public static void main(String arg[])throws IOException{
      File f = new File("ddd.txt");
      FileInputStream fis = new FileInputStream(f);
      BufferedInputStream bis = new BufferedInputStream(fis);
      ObjectInputStream ois = new ObjectInputStream(bis);
     
      Object obj = null;
      try{
       obj = ois.readObject();
      }catch(ClassNotFoundException e){}
     
      AAA ap = (AAA)obj;
      System.out.println(ap.x);
      System.out.println(ap.y);
      System.out.println(ap.z);
     }
    }

Designed by Tistory.