객체를 파일로 출력하기 위해  FileOutputStream 객체를 하나 만들고 writeObject() 함수를 이용해서 
 출력을 시키는 함수를 하나 만들었습니다.
그런데 FileOutputStream 객체를 만들때 FileOutputStream(FileName,true) 생성자에 true값을 주어
해당 함수가 불려질때 마다 객체를 이미 있는 파일 뒤에 이어서 저장되도록 하였습니다.

그리고 FileInputStream 객체를 만들고 readObject() 함수를 이용해서
저장한 객체를 읽어들이는 함수 만들었습니다.

 

그런데 readObject를 함수를 이용하면 파일에 제일 처음에 저장된 객체만 리턴되어 돌아외고

다음번 readObject함수부터는 다음번에 저장된 객체가 읽혀 오지 않고 에러가 발생합니다.

 

파일에 저장된 객체 정보를 계속적으로 읽어오게 할려면 어떻게 해야 하나요?

 

밑에 코드가 제가 작성한 코드입니다.

public void SaveToFile(Schedule_Data sd)
 {
  System.out.println(\"SaveToFile start..\");  
   
    String FileName = new String(\"d:/schedule.txt\");   //저장위치 및 파일명
    FileOutputStream fout = null;
   
    ObjectOutputStream out = null;
    try {
    
     fout = new FileOutputStream(FileName,true);
     out = new ObjectOutputStream(fout);
    
     out.writeObject(sd);
   
     out.flush();
     out.close();
     fout.close();
    
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }
 
private void ReadFromFile()
 {  
  System.out.println(\"ReadFromFile start..\"); 
  
  String FileName = new String(\"d:/schedule.txt\");   //읽어올 파일명
  FileInputStream fin = null;
  ObjectInputStream in = null;
  Schedule_Data[] sd = new Schedule_Data[10];
  
  try {

   fin = new FileInputStream(FileName);
   in = new ObjectInputStream(fin);   

   try{  
       
     Schedule_Data temp;
     int i=0;
    
    while((temp =(Schedule_Data)in.readObject())!=null)
     {
      sd[i] = new Schedule_Data();
      sd[i] = temp;
     
      i++;
     }
   
   }catch(ClassNotFoundException e)
   {
    e.printStackTrace();
   }   
     
   in.close();
   fin.close();

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }