Discussions

General J2EE: delete a folder with files inside

  1. delete a folder with files inside (3 messages)

    Hi there,
    does anyone know if there's a method wich allows me to delete a complete folder with files inside (obviously I also need to delete the files)?
    Thanks in advance

    Threaded Messages (3)

  2. Here's one: http://www.koders.com/java/fid632E382138FCCDF61797E004DD2B8FC9913E236E.aspx

    -- Rob
    http://www.robsite.org
  3. Here's another simplier one from: http://javaalmanac.com/egs/java.io/DeleteDir.html

        // Deletes all files and subdirectories under dir.
        // Returns true if all deletions were successful.
        // If a deletion fails, the method stops attempting to delete and returns false.
        public static boolean deleteDir(File dir) {
            if (dir.isDirectory()) {
                String[] children = dir.list();
                for (int i=0; i<children.length; i++) {
                    boolean success = deleteDir(new File(dir, children[i]));
                    if (!success) {
                        return false;
                    }
                }
            }
        
            // The directory is now empty so delete it
            return dir.delete();
        }
  4. re:[ Go to top ]

    first I would thank you for your help.
    I apreciated your code examples.
    But as I am a newbie, I wonder what would happens if I call a File object constructor with a path name that match with a file wich already existing.
    Would the constructor overwrite it? Or just launch an IO ex?
    Thanks again.