2011-03-12 2 views
0

현재 사람들에 관한 데이터를 .dat 파일에 저장하는 프로그램을 작성하려고합니다. 프로그램은 항목을 추가하고, 항목을 표시하고, 항목을 삭제할 수 있어야합니다 (모두 동일한 유형의 오브젝트 임).ObjectOutputStream을 사용하여 특정 데이터 삭제

내가 겪고있는 문제는 항목을 삭제하는 것입니다. 나는 특별히 랜덤 액세스 파일을 사용하지 말라는 지시를 받았지만 데이터의 특정 위치를 삭제할 수 있어야합니다. 어떤 종류의 포인터를 제어 할 방법이 없기 때문에 삭제할 항목을 정의하는 방법에 대해 확신 할 수 없습니다. 항목의 위치를 ​​찾는 방법이 있지만 객체 위치에서 데이터를 삭제할 방법이 없습니다.

private static Person findPerson(String theName) 
{ 
    Person thePerson = null; 

    try 
    { 
     inputStream = new ObjectInputStream(new FileInputStream(personFile)); 

     while(!done) //While the name has not been found 
     { 
      thePerson = (Person)inputStream.readObject(); 

      if(theName.equals(thePerson.getName())) //If the name is found 
      { 
       done = true; 
      } 
     } 
     done = false; 

     inputStream.close(); //Close the stream 
    } 

    catch(IOException e) //If the end of the file is reached 
    { 
     System.out.println("The name you specified is not in the file."); 
    } 
    catch(ClassNotFoundException e) //If there is trouble 
    { 
     System.out.println("Problems with file input."); 
    } 

    return thePerson; //Return the record 
} 

ObjectOutputStream에서 데이터를 삭제해야하는 위치를 정의 할 수있는 방법이 있습니까? 특정 항목을 삭제하는 방법은 무엇입니까?

답변

1

언제든지 새로운 임시 파일을 만들고 while 루프에서 관심있는 항목 만 추가 할 수 있습니다. 이전 파일을 제거하고 임시 파일 이름을 변경하면

관련 문제