closing the use of a resource
Hey,
I have an address book application that has save,delete and get addressentry functions and uses persistence.
the problem is when I close the use of the file that the save and get addressentry functions use after they are done with it, the application crashes if I try and create the same address twice.
And if I dont close the use of the file,there are no errors but the delete function cannot delete the file since the other functions are still using it.
Please help,this assignment was due like yesterday.
the get addressbookentry function,gets the address to delete, edit or view
Code:
public AddressBookEntry getAddressBookEntry(String name)
throws AddressBookDelegateException {
String nameWithUnderscores = replaceSpacesWithUnderScores(name);
File fileToUnserialize =
new File(addressBookDirectory, nameWithUnderscores);
if (!fileToUnserialize.exists()) {
return null;
}
try {
InputStream inputStream = new FileInputStream(fileToUnserialize);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
AddressBookEntry addressBookEntry =
(AddressBookEntry) objectInputStream.readObject();
objectInputStream.close();
return addressBookEntry;
} catch (FileNotFoundException e) {
throw new AddressBookDelegateException(
"Could not find the following file to deserialize, "
+ fileToUnserialize.getAbsolutePath(), e);
} catch (IOException e) {
throw new AddressBookDelegateException(
"Could not read inputStream of file, "
+ fileToUnserialize.getAbsolutePath(), e);
} catch (ClassNotFoundException e) {
throw new AddressBookDelegateException(
"Could not find class, " + AddressBookEntry.class
+ " to desrialize file, "
+ fileToUnserialize.getAbsolutePath(), e);
}
}
the save address function, saves an address
Code:
public void saveAddressBookEntry(AddressBookEntry addressBookEntry)
throws AddressBookDelegateException {
String nameWithUnderscores =
replaceSpacesWithUnderScores(addressBookEntry.getName());
File fileToSerialize =
new File(addressBookDirectory, nameWithUnderscores);
if (fileToSerialize.exists()) {
if (fileToSerialize.delete()) {
throw new AddressBookDelegateException(
"Was trying to save file, "
+ fileToSerialize.getAbsolutePath() + " and found "
+ "file already existed. Tried to delete it "
+ "but failed.");
}
} else {
try {
OutputStream outputStream =
new FileOutputStream(fileToSerialize);
ObjectOutputStream objectOutputStream =
new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(addressBookEntry);
objectOutputStream.close();
} catch (FileNotFoundException e) {
throw new AddressBookDelegateException(
"Could not find file, "
+ fileToSerialize.getAbsolutePath()
+ " to serialize to", e);
} catch (IOException e) {
throw new AddressBookDelegateException(
"Could not serialize addressBookEntry to "
+ "file, " + fileToSerialize.getAbsolutePath(), e);
}
}
}
the delete address function,deletes an address.
Code:
public void deleteAddressBookEntry(String name)
{
String nameWithUnderscores = replaceSpacesWithUnderScores(name);
File fileToDelete =
new File(addressBookDirectory, nameWithUnderscores);
System.out.println("Deleting " + fileToDelete.getPath());
if
(fileToDelete.exists()) {
fileToDelete.delete();
}
else if(!fileToDelete.exists())
{
System.out.println("File does not exist");
}
}