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' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -