Results 1 to 11 of 11
  1. #1
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default 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();
            }
        }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default

    Does fileToDelete.delete() return true?

    db

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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]
    }

  5. #5
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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?

  7. #7
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    As in i want to delete one person's address not the entire address book..

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

  9. #9
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    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?

  10. #10
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    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.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. Deleting '.zip' file...
    By vaibhavspawar in forum New To Java
    Replies: 2
    Last Post: 07-02-2010, 12:13 PM
  2. File Not Deleting
    By Moncleared in forum New To Java
    Replies: 7
    Last Post: 02-21-2010, 09:28 PM
  3. Help deleting a file
    By 3speed in forum New To Java
    Replies: 4
    Last Post: 11-01-2008, 06:27 AM
  4. JDO - Retrieving a persisted object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-17-2008, 08:46 AM
  5. Deleting a File that is opened
    By ravian in forum Advanced Java
    Replies: 6
    Last Post: 01-30-2008, 03:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •