Home arrow static arrow Java Programming [Archive] - File.listFiles() does not list all files/directories
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - File.listFiles() does not list all files/directories
This topic has 3 replies on 1 page.

Posts:3,369
Registered: 24.10.97
File.listFiles() does not list all files/directories  
Jul 30, 2004 6:02 AM



 
I wrote a utility method to delete a directory (that can contain subdirectories/files:

    /**     * Delete a directory including all of its content.     *      * @param directory Directory path to delete.     * @return False if at least one file can not be deleted.     */    static public boolean deleteDirectory(String directory) {        boolean result = false;                if (directory != null) {            File file = new File(directory);            if (file.exists() && file.isDirectory()) {                //1. delete content of directory:                File[] files = file.listFiles();                result = true; //init result flag                int count = files.length;                for (int i = 0; i < count; i++) { //for each file:                    File f = files[i];                    if (f.isFile()) {LOGGER.info("Delete file '"+f+"' ...");boolean fres = f.delete();LOGGER.info("... deletion: "+fres);result = result && fres;                        ICUtils.waitSeconds(1);                    } else if (f.isDirectory()) {                        result = result && deleteDirectory(f.getAbsolutePath());                    }                }//next file                                result = result && file.delete(); //finally delete (empty) input directory            }//else: input directory does not exist or is not a directory        }//else: no input value                return result;    }//deleteDirectory()


but i noticed, that not all files are deleted. During debugging, I noticed that the listFiles() method doesn't return all files and directories of a directory - althought there are no special files or directories in there (hidden, write protected or similar ... when deleting with windows (select + DEL), this works).

Is listFiles() broken?
 

Posts:3,369
Registered: 24.10.97
Re: File.listFiles() does not list all files/directories  
Jul 30, 2004 6:42 AM (reply 1 of 3)



 
Well, it was'nt the problem of listFiles. It was the problem of the

result = result && deleteDirectory(f.getAbsolutePath());

assignment. If result is false, the JVM does not execute the following deleteDirectory(f.getAbsolutePath());
of this bool comparisson. Better:

result = deleteDirectory(f.getAbsolutePath()) && result;

which ensures that as much files/directories will be deleted as possible.
 

Posts:31,095
Registered: 4/30/99
Re: File.listFiles() does not list all files/directories  
Jul 30, 2004 8:08 AM (reply 2 of 3)



 
Or use & instead of &&.
 

Posts:3,369
Registered: 24.10.97
Re: File.listFiles() does not list all files/directories  
Aug 2, 2004 1:33 AM (reply 3 of 3)



 
Thanks for the hint. I just learned about the difference between &, ^, | and &&, ||
 
This topic has 3 replies on 1 page.