I am currently trying to write a program to store data about people into a .dat file. The program must be able to add entries, display entries, and delete entries (all of which are an object of the same type).
The trouble I'm running into is with deleting entries. I have been specifically instructed to NOT use a random access file, but I need to be able to delete specific locations of data. I'm not sure how I can go about defining which entries to delete, as I have no way of controlling any sort of pointer. I have a method to find the location of an entry, but I have no way to delete the data at the location of the object.
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
}
Is there a way for me to define where an ObjectOutputStream should go to delete data? How would I go about deleting a specific entry?
You can always create new temporary file and in while loop adding there only entries which you are interested in. After it removes old file and change temporary file name