Results 1 to 4 of 4

Thread: Delete direcory

  1. #1
    pinar.n is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Question Delete direcory

    hi every body
    i want to delete directory that may contain another directory or text file i use this recursive code when i send a directory that contain 2 text file method delete the second element in array and do not delete the first file and i do not know why?

    Java Code:
        public boolean delete(File f) {
    
    
            if (f.exists()) {
                File[] files = f.listFiles();
    
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        System.out.println(delete(files[i]));
                    } else {
    
                        System.out.println(files[i].delete());
                    }
    
                }
    
            }
            return (f.delete());
        }
    Last edited by JosAH; 07-04-2011 at 05:37 PM. Reason: added [code] ...[/code] tags

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    You program logic is incorrect: if f exists it doesn't have to be a directory, it can be an ordinary file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    @Jos

    That was my initial thought. Then I assumed that the initial call to the method is made with a known directory.

    @OP

    If you code is unable to delete a file then you may not have permissions to delete it or that some program (possibly the one you are currently running) has the file open. Make sure all files are closed before you try deleting them.

  4. #4
    pinar.n is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    thanks
    i have open files in my program so that method do not delete them
    good luck

Similar Threads

  1. Replies: 2
    Last Post: 04-20-2009, 08:00 AM
  2. help to delete
    By andykots in forum Advanced Java
    Replies: 1
    Last Post: 01-29-2009, 08:29 AM
  3. Delete
    By Sarinam in forum New To Java
    Replies: 6
    Last Post: 07-23-2008, 11:09 AM
  4. Delete From .txt file
    By Sarinam in forum New To Java
    Replies: 86
    Last Post: 06-28-2008, 10:17 AM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 08:11 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
  •