Java code to delete a file is not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ganeshp
    New Member
    • May 2007
    • 38

    Java code to delete a file is not working

    Code to delete the file is given below:

    File file = new File(filePath);
    file.delete();

    But this code does not delete the file.
    Please post your thoughts on this issue.

    Regards,
    Ganesh
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What does the method return?

    kind regards,

    Jos

    Comment

    • ganeshp
      New Member
      • May 2007
      • 38

      #3
      Originally posted by JosAH
      What does the method return?

      kind regards,

      Jos
      I have not checked its return value.

      Comment

      • ganeshp
        New Member
        • May 2007
        • 38

        #4
        What is it returns false , even if the file exists.

        My application generates many intermediate files , i want to delete them as and when i complete using them.

        Please post ways to delete a file.
        Please let me know your thoughts on this.

        Regards,
        Ganesh

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by ganeshp
          I have not checked its return value.
          Oh. I'm curious why you haven't, since you are having trouble deleting.

          Another method that may be useful to you is File's deleteOnExit() method. This method is handy for temporary files, but read its documentation.

          Comment

          • id62000
            New Member
            • Dec 2009
            • 1

            #6
            The problem is probably because the file is open. If at some point in your program the file is opened, make sure its closed before deleting it.
            eg:
            FileInputStream in = new FileInputStream ("c:/a.txt");
            make sure you have
            in.close();
            before using delete.

            Comment

            • pbrockway2
              Recognized Expert New Member
              • Nov 2007
              • 151

              #7
              Originally posted by id62000
              The problem is...
              The problem was...

              This thread is very, very old. Quietly sleeping. Possibly dead; certainly best left alone.

              Comment

              • AnnoyedAtYou
                New Member
                • Sep 2022
                • 1

                #8
                @pbrockway2

                Certainly not best left alone. A late reply might not help the person that asked, but it doesn't mean it won't help anyone. So, get your head out of your a** and stop telling people off for being helpful.

                Comment

                Working...