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

    Default 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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    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.

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

    Default

    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


    Java 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");
        }
            
        }
    the code above cant run immediately after the code below has executed rather I have to run the program again
    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);
                }
            }
    
                }
            }
    please ignore the fact that there might be more braces than need be Ive left out some exception handling.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Where do you close the file?

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

    Default

    close the file how exactly?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Read the API doc for the file I/O classes you are using.

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

    Default

    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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Glad to help.

    Pass it on when you get a chance. Many new programmers forget to close files leaving it to the JVM to take care of things when the program ends. Sometimes you must to do it to get the program to work. Better to always close when done.

Similar Threads

  1. Deleting persisted file
    By jayragz in forum New To Java
    Replies: 10
    Last Post: 05-12-2011, 10:40 PM
  2. Replies: 0
    Last Post: 07-15-2010, 01:23 PM
  3. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  4. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  5. JDO - Retrieving a persisted object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-17-2008, 07:46 AM

Posting Permissions

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