Results 1 to 11 of 11
Thread: Deleting persisted file
- 05-12-2011, 08:16 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 35
- Rep Power
- 0
Deleting persisted file
Hi,
I have an address book application and Im creating the functionality to delete an address entry.
The application saves the entries to a file on my hard drive through java persistence.
Everything is working fine but the delete function.
After i choose the delete option in the command-line gui it asks for the name of the person who's entry I want to delete just like I want it to but I after I enter the name of the person,It doesnt do so and instead brings back the previous menu.
the delete address entry method is shown below;
Java Code:public void deleteAddressBookEntry(AddressBookEntry addressBookEntry) { String nameWithUnderscores = replaceSpacesWithUnderScores(addressBookEntry.getName()); File fileToDelete = new File(addressBookDirectory, nameWithUnderscores); if (fileToDelete.exists()) { fileToDelete.delete(); } }
- 05-12-2011, 08:28 AM #2
It's not very clear but to me it seems that if you want to delete "Joe Smith" from the address book your code is actually trying to delete a file called "Joe Smith". If you want to delete a single entry from the address book you need to open (read) the file, delete the entry and write the contents back to the file.
- 05-12-2011, 08:31 AM #3
Does fileToDelete.delete() return true?
db
- 05-12-2011, 08:31 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
instead brings back the previous menu.
That happens in code you haven't posted.
You could add code to report what happens in that method:
Java Code:public void deleteAddressBookEntry(AddressBookEntry addressBookEntry) { String nameWithUnderscores = replaceSpacesWithUnderScores( addressBookEntry.getName()); File fileToDelete = new File(addressBookDirectory, nameWithUnderscores); [color=blue]System.out.println("Deleting " + file.getPath());[/color] if(fileToDelete.exists()) { [color=blue]boolean result = [/color]fileToDelete.delete(); [color=blue]Sstem.out.println("deletion sucessful? " + result);[/color] } [color=blue]else { System.out.println("File does not exist"); }[/color] }
- 05-12-2011, 08:42 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 35
- Rep Power
- 0
Thanx yall for the posts.Really appreciated.
Ive tried what pbrockway2 suggested;
It brings up the correct file path but the result is false and therefore doesnt delete the file.
I really don't understand why since im trying to delete the entire file and not just an entry inside it.
- 05-12-2011, 08:48 AM #6
Then why is the method called "deleteAddressBookEntry" and not "deleteAddressBook"?
Why are you trying to delete a file with a name based upon a single entry and not the actual address book file?
- 05-12-2011, 09:29 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 35
- Rep Power
- 0
As in i want to delete one person's address not the entire address book..
- 05-12-2011, 09:45 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
Are you saying that the directory represents the address book (the collection of names etc) and that each file is being treated as an entry?
As far as the result being null is concerned, can you delete the files "manually" rather than from within the program? Obviously for result to be true you have to have the file system rights to delete the file and deleting them by hand would verify that.
Another thing that can go wrong is that files may be locked by the program. So try deleting the files while the program is actually running. If your program is actually creating the files and writing their contents, make sure you close any writer or stream that you are using.
- 05-12-2011, 02:09 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 35
- Rep Power
- 0
Yes,the directory represents the address book and each person's address is being treated as an entry.
Manually I can delete the files but I can't do it with the program.
The parent folder(ADDRESS_BOOK)is however read only and i keep trying to change that but it keeps reverting.Could that be the problem?
- 05-12-2011, 04:56 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 35
- Rep Power
- 0
Realized where I was going wrong, I was using a get_address method I had created for viewing the addresses in the delete functionality of the program and it was locking up the entry I was trying to delete..
thank you all for the input.
- 05-12-2011, 10:40 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
Similar Threads
-
Deleting '.zip' file...
By vaibhavspawar in forum New To JavaReplies: 2Last Post: 07-02-2010, 11:13 AM -
File Not Deleting
By Moncleared in forum New To JavaReplies: 7Last Post: 02-21-2010, 08:28 PM -
Help deleting a file
By 3speed in forum New To JavaReplies: 4Last Post: 11-01-2008, 05:27 AM -
JDO - Retrieving a persisted object
By Java Tip in forum Java TipReplies: 0Last Post: 03-17-2008, 07:46 AM -
Deleting a File that is opened
By ravian in forum Advanced JavaReplies: 6Last Post: 01-30-2008, 02:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks