java - Why is File::listFiles faster than DirectoryStream - accepting only directories -


so "heard" directorystream introduced in java 7 faster traditional methods of directory listing. however, not case me. might have been faster listing whole directory, when filtering out files , accepting folders, takes far more time. instance, these codes:

using file:

arraylist<file> temparray = new arraylist(); (file file : somefile.listfiles()){     if(!file.isdirectory())         continue;     temparray.add(file); } 

using directorystream:

directorystream<path> stream = null; try {     stream = files.newdirectorystream(dir, new directorystream.filter<path>() {             public boolean accept(path file) throws ioexception {                 return files.isdirectory(file);             }         }); } catch (ioexception ex) { }  arraylist<file> files = new arraylist(); (path path : stream)     files.add(path.tofile()); stream.close(); 

the second method (using directorystream) more slower. know both methods check each , every file see if directory. or because directorystreamis synchronized (again, correct me if wrong)?

well, in second case you're testing twice path directory. further, you're defining new class, has relatively large first-use cost.


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -