Results 1 to 19 of 19
  1. #1
    Phazon1704 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default File not deleting

    I'm trying to delete a file in Java and using the following code to see if it was successful:

    Java Code:
                                                    EraseComplete = TempBookingFile.delete();
                                                    if (EraseComplete)
                                                    {
                                                        System.out.println("The file was deleted successfully.");
                                                    }
                                                    else
                                                    {
                                                        System.out.println("Error: The file was not deleted.");
                                                    }
                                                    TempBookingFile.createNewFile();
    The problem is that the file isn't deleting for the most-part. What I'm noticing is that it'll delete sometimes and other times it won't, but once it fails to delete it will never be successful after that and the only way I can get it to work again is to manually delete the file and run the program again. I'm really just wondering what the likely causes of this could be. I could post more code if that'd help to identify the problem, but it might be a bit lengthy since I'm not sure which part of the code I'd need to post.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: File not deleting

    the likely causes of this could be.
    The file is still open.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Phazon1704 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: File not deleting

    I don't think that's the problem, because right before that code I posted I have this:

    Java Code:
                                                    toFile.close();
                                                    fromFile.close();
                                                    toTempFile.close();
                                                    fromTempFile.close();
    EDIT:

    Unless this part isn't working. Could I check this in the same way as I did with the file erase?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: File not deleting

    Can you make a small, complete program that compiles, executes and shows the problem? Don't post the whole project which must have lots of code not relevant to the problem.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: File not deleting

    File doesn't have a close() method. You need to be much more specific in order to get meaningful help.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Phazon1704 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: File not deleting

    Sorry, toFile and toTempFile are BufferedWriters and fromFile and fromTempFile are BufferedReaders.

    EDIT:

    In a bit more detail, I declare these at the start of the program:

    Java Code:
            //Variables for file-handling
            FileWriter FileOutputUNIP = null;
            FileInputStream FileInputUNIP = null;
            FileWriter FileOutputBOOKING = null;
            FileInputStream FileInputBOOKING = null;
            FileWriter FileOutputTEMPBOOKING = null;
            FileInputStream FileInputTEMPBOOKING = null;
            DataInputStream streamIn = null;
            DataInputStream streamTempIn = null;
            BufferedWriter toFile = null;
            BufferedWriter toTempFile = null;
            BufferedReader fromFile = null;
            BufferedReader fromTempFile = null;
    Every time I read an input from the user (The program is based on users typing certain commands) I set the following:

    Java Code:
                        //Set up booking files
                        FileOutputBOOKING = new FileWriter(BookingFile, true);
                        toFile = new BufferedWriter(FileOutputBOOKING);
                        FileOutputTEMPBOOKING = new FileWriter(TempBookingFile, true);
                        toTempFile = new BufferedWriter(FileOutputTEMPBOOKING);
                
                        //Set up file reading variables
                        FileInputBOOKING = new FileInputStream(BookingFile);
                        streamIn = new DataInputStream(FileInputBOOKING);
                        fromFile = new BufferedReader(new InputStreamReader(streamIn));
                        
                        //Set up file reading variables
                        FileInputTEMPBOOKING = new FileInputStream(TempBookingFile);
                        streamTempIn = new DataInputStream(FileInputTEMPBOOKING);
                        fromTempFile = new BufferedReader(new InputStreamReader(streamTempIn));
    When a chain of commands are input properly by the user, the contents of the file are written to a temporary file with the necessary change made:

    Java Code:
                                                    //Close booking file streams and delete file
                                                    toFile.close();
                                                    fromFile.close();
                                                    EraseComplete = BookingFile.delete();
                                                    //Check if file deletion was successful
                                                    if (EraseComplete)
                                                    {
                                                        System.out.println("The file was deleted successfully.");
                                                    }
                                                    else
                                                    {
                                                        System.out.println("Error: The file was not deleted.");
                                                    }
                                                    //Close temporary file input stream
                                                    toTempFile.close();
                                                    //Create new empty booking file
                                                    BookingFile.createNewFile();
                                                    //Set up new booking file to read in data
                                                    FileOutputBOOKING = new FileWriter("BOOKING.txt", true);
                                                    toFile = new BufferedWriter(FileOutputBOOKING);
    After this the main file should be deleted and the altered contents of the temporary file are meant to be written to the main file:

    Java Code:
                                                    //Close booking file streams and delete file
                                                    toFile.close();
                                                    fromFile.close();
                                                    EraseComplete = BookingFile.delete();
                                                    //Check if file deletion was successful
                                                    if (EraseComplete)
                                                    {
                                                        System.out.println("The file was deleted successfully.");
                                                    }
                                                    else
                                                    {
                                                        System.out.println("Error: The file was not deleted.");
                                                    }
                                                    //Close temporary file input stream
                                                    toTempFile.close();
                                                    //Create new empty booking file
                                                    BookingFile.createNewFile();
                                                    //Set up new booking file to read in data
                                                    FileOutputBOOKING = new FileWriter("BOOKING.txt", true);
                                                    toFile = new BufferedWriter(FileOutputBOOKING);
    Lastly the I want to close all streams and delete the temporary file:

    Java Code:
                                                    //Close all streams
                                                    toFile.close();
                                                    fromFile.close();
                                                    toTempFile.close();
                                                    fromTempFile.close();
                                                    //Delete the temporary file
                                                    EraseComplete = TempBookingFile.delete();
                                                    //Check if file deletion was successful
                                                    if (EraseComplete)
                                                    {
                                                        System.out.println("The file was deleted successfully.");
                                                    }
                                                    else
                                                    {
                                                        System.out.println("Error: The file was not deleted.");
                                                    }
                                                    //Create a new empty temportary file
                                                    TempBookingFile.createNewFile();
    By the end the main file should have the altered contents and the temporary file should be empty. I'm pretty sure it would work if the files would delete successfully.
    Last edited by Phazon1704; 08-28-2012 at 07:46 PM. Reason: formatting

  7. #7
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: File not deleting

    This may sound stupid but you have got nothing to lose. Just prior to trying to delete the file perform
    Java Code:
    System.gc();
    If this solves the problem then OK but if not then, even though you think you are, you are probably not closing the files; the most likely reason being that you are smothering an exception.

  8. #8
    Phazon1704 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: File not deleting

    I gave that a go and as far as I can tell, the issue has been resolved. There was one occasion where the file wasn't deleted, but I had the .txt open accidentally. Thanks for the help :).

    EDIT:

    Never mind it still doesn't work properly. It certainly works a lot more often, but it's not entirely resolved. What do you mean by smothering an exception?
    Last edited by Phazon1704; 08-28-2012 at 08:24 PM.

  9. #9
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: File not deleting

    Quote Originally Posted by Phazon1704 View Post
    What do you mean by smothering an exception?
    Catching an exception but doing nothing at all with it; not even printing the stack trace. Very common with newcomers to exceptions and is the equivalent of ignoring function return codes in 'C' .

  10. #10
    Phazon1704 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: File not deleting

    Ah, all the code I wrote before is in one big try-catch:

    Java Code:
    try {
            //code
                    } catch (IOException error) {
                        System.out.println("Error reading from client socket" + clientSocket);
                    }
    The IOException error is highlighted as a warning, which says "Variable error isn't used" - Is this what you're referring to? If so, what can I do to resolve this issue?

  11. #11
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: File not deleting

    Quote Originally Posted by Phazon1704 View Post
    Ah, all the code I wrote before is in one big try-catch:

    Java Code:
    try {
            //code
                    } catch (IOException error) {
                        System.out.println("Error reading from client socket" + clientSocket);
                    }
    The IOException error is highlighted as a warning, which says "Variable error isn't used" - Is this what you're referring to? If so, what can I do to resolve this issue?
    Apart from printing an error message that is just ignoring the exception and throwing away ALL diagnostic information. Why are you catching the exception? I can't see the detail of your code but it looks to me like recovery from this exception is not possible so it should not be caught.

    There are two primary reasons to catch exceptions - first to program some recovery and second to convert the exception to a user defined exception that wraps the caught exception and to throw that.

  12. #12
    Phazon1704 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: File not deleting

    Okay I think I see what you're saying about there being no need for the try-catch, but I tried to remove it and there is an error on all code that was previously in the try statement, which says "unreported exception java.io.IOException; must be caught or declared to be thrown". I think this is why the try-catch is there, but I altered the code in the catch a little to include the actual error itself just to get some use from it.

    EDIT:

    Could the issue with the try-catch be the cause of the file deletion not working properly?
    Last edited by Phazon1704; 08-28-2012 at 11:13 PM.

  13. #13
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: File not deleting

    Quote Originally Posted by Phazon1704 View Post
    Okay I think I see what you're saying about there being no need for the try-catch, but I tried to remove it and there is an error on all code that was previously in the try statement, which says "unreported exception java.io.IOException; must be caught or declared to be thrown". I think this is why the try-catch is there, but I altered the code in the catch a little to include the actual error itself just to get some use from it.
    The "unreported exception java.io.IOException; must be caught or declared to be thrown" can be handled by making the method you are running this in declare that it throws an IOException. Sometimes this is not possible, for example when you are implementing an interface that does not declare that, then you need to wrap the exception in an exception you can throw; you can always throw an Error which is constructed with a message and the IOException.

    If everything is under your control then one has to assess each exception possibility and decide whether or not you can sensibly recover from it or not. If you can't then it is normally best to pass the exception back. It is very common to see people new to exceptions either catching every exception as early as possible or never catching any and passing them all back.

    The actual error message is of little use without the stack trace.

    EDIT:

    Could the issue with the try-catch be the cause of the file deletion not working properly?
    Only if your own error message was printed to the console.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: File not deleting

    Since the gc() call improved things, can I suggest breaking your code into smaller methods?
    Ensure all the reading and writing is done in a single method (including opening and closing streams) and that those variables go out of scope, that is they are local.
    Once that method is done then call another one that erases the current temporary file.

    I'm wondering if, because the gc call helped, this is simply a scoping issue around those streams and they aren't always eligible for collection?
    Of course that does imply that the file handling code between the JVM and the OS is a bit flaky, but this deletion problem has been seen before.
    Out of curiosity, what OS are you on?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Phazon1704 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: File not deleting

    I tried what you suggested, but I must have misunderstood, because the file deletion is now failing every single time. I broke my code into methods and have ones for copying to the temporary file, copying to the main file and deleting either file, but I'm not sure if I fully understand what you mean when you say put the variables out of scope. I've just been feeding the methods the same variables as before, and I'm not sure if that's what you meant. I'm on Windows 7 Home Premium 64-bit, by the way.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: File not deleting

    No.
    Don;t pass the variables.
    They should be defined in the method:
    Java Code:
    public void copyStuff(File from, File temp) {
    // open streams.
    // copy stuff (or whatever it s you;re doing)
    }
    ...
    public void methodThatDoesTheWork() {
        File from = new File(etc etc);
        File temp = new File(etc etc);
        copyStuff(from, temp);
        ...
        temp.delete();
    }
    This way the readers/writers etc definitely go out of scope.
    You want to write the code in such a way that you ensure they aren't open for more time than they are actually being used, and that the objects representing those streams go out of scope.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    Phazon1704 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: File not deleting

    The same variables are used in other places in the code, won't there be errors if i declare them in a new method?

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: File not deleting

    Possibly.
    Take into account we haven't a clue what your system does.
    If you are holding open streams throughout your program then I would consider revisiting your design, as that could well be the root of your problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    Phazon1704 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: File not deleting

    Okay I think I've got this problem sorted now - Just wanted to thank you all for your help.

Similar Threads

  1. deleting text file
    By droidus in forum New To Java
    Replies: 18
    Last Post: 01-21-2012, 02:11 AM
  2. Deleting persisted file
    By jayragz in forum New To Java
    Replies: 10
    Last Post: 05-12-2011, 11:40 PM
  3. Deleting '.zip' file...
    By vaibhavspawar in forum New To Java
    Replies: 2
    Last Post: 07-02-2010, 12:13 PM
  4. File Not Deleting
    By Moncleared in forum New To Java
    Replies: 7
    Last Post: 02-21-2010, 09:28 PM
  5. Help deleting a file
    By 3speed in forum New To Java
    Replies: 4
    Last Post: 11-01-2008, 06:27 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
  •