Results 1 to 8 of 8
Thread: unlocking persisted file
- 05-12-2011, 06:14 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 35
- Rep Power
- 0
unlocking persisted file
Hi,
I have an address book application that can add,view and delete an address.
The delete function works okay unless I have just added the address I want to delete.
In order to delete an address I have just added,I have to close the program and run it again.
Is there someway to get past this problem?
- 05-12-2011, 06:26 PM #2
Can you give some more details about where and how the data is being held?
If needed can you write a short program that adds and then tries to delete a simple entry to demonstrate the problem.
- 05-12-2011, 06:39 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 35
- Rep Power
- 0
The data is being held in a file on the hard drive called ADDRESS_BOOK.
Inside ADDRESS_BOOK there are address entries for different individuals.
My problem is;
Once I create a new address,I can't delete it immediately using the delete function of the program,rather I have to close the program and run it again so as to delete the just created address i.e
the code above cant run immediately after the code below has executed rather I have to run the program againJava 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()) { boolean result = fileToDelete.delete(); System.out.println("deletion sucessful? " + result); } else if(!fileToDelete.exists()) { System.out.println("File does not exist"); } }
please ignore the fact that there might be more braces than need be Ive left out some exception handling.Java Code:public void saveAddressBookEntry(AddressBookEntry addressBookEntry) { String nameWithUnderscores = replaceSpacesWithUnderScores(addressBookEntry.getName()); File fileToSerialize = new File(addressBookDirectory, nameWithUnderscores); if (fileToSerialize.exists()) { else { try { OutputStream outputStream = new FileOutputStream(fileToSerialize); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(addressBookEntry); } 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); } } } }
- 05-12-2011, 06:41 PM #4
Where do you close the file?
- 05-12-2011, 06:52 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 35
- Rep Power
- 0
close the file how exactly?
- 05-12-2011, 06:57 PM #6
Read the API doc for the file I/O classes you are using.
- 05-12-2011, 07:25 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 35
- Rep Power
- 0
I read it and closed the use of the resource after the save address was done with it and it worked.
Thanks alot for your help,it was very appreciated.
Maybe someday I can be of help to someone who has the same problem.
- 05-12-2011, 07:43 PM #8
Similar Threads
-
Deleting persisted file
By jayragz in forum New To JavaReplies: 10Last Post: 05-12-2011, 10:40 PM -
Log4J : logs of a specific category to a file, but only errors to the file AND stdout
By msegmx in forum Advanced JavaReplies: 0Last Post: 07-15-2010, 01:23 PM -
Sending a File from Server to Client and saving the file to Clients computer
By al_Marshy_1981 in forum NetworkingReplies: 8Last Post: 02-18-2010, 12:54 PM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
JDO - Retrieving a persisted object
By Java Tip in forum Java TipReplies: 0Last Post: 03-17-2008, 07:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks