Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest possibility of listing a directory and getting the URLs of every file in Java

Tags:

java

file

url

i am planning to perform a standard list command to get a vector or a list of the content of a directory.

I know this is easy by using

File f = new File("C:/testDir");
File[] files = f.listFiles();

The problem is that I need a list/array/vector of URLs. So my thoughts were to convert the files to URL. With the org.apache.commons.io.FileUtils library this is possible with the following simple code:

URL[] urls = FileUtils.toURLs(files);

This does exactly what I need, but is unfortunately very slow (especially for directories with thousands of files), although it is just using a for-loop and parses every single File object with the "toURL()" method.

Does someone know a way to do this task in a better performance?

like image 236
Ahaggar Avatar asked Oct 05 '10 13:10

Ahaggar


2 Answers

The only optimization that is simple would be reducing object creation, which will make a modest improvement in performance. Instead of using listFiles(), which creates a whole slew of File objects, use list() to get a String array of just the file names, not the paths, and create the URLs directly. String creation and storage will have less object overhead in this case. The string manipulation could obviously be made faster and more proper as well, although it probably won't make a huge difference.

Something like:

ArrayList<URL> urls = new ArrayList<URL>(); //or use an array if you prefer.
for(String name: f.files())
    urls.add(new URL("file://"+f.getPath()+"/"+name));
like image 198
Peter DeWeese Avatar answered Sep 28 '22 07:09

Peter DeWeese


Create a new URL object, instead of invoke the toUrl() method seems to be more efficient. I have checked this out:

    File parent=new File("./doc");
    File[] listado=parent.listFiles();
    long t0=0L;
    try {
       t0=System.currentTimeMillis();
       for(int k=0;k<10000;k++) {
        URL[] listaArchivos=new URL[listado.length];
        for (int i = 0; i < listado.length; i++) {
            listaArchivos[i]=listado[i].toURL();
        }
       } 
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Files:"+listado.length+"; Time 1: "+(System.currentTimeMillis()-t0)+" ms");


    try {
        t0=System.currentTimeMillis();
        for(int k=0;k<10000;k++) {
            URL[] listaArchivos=new URL[listado.length];
            for (int i = 0; i < listado.length; i++) {
                listaArchivos[i]=new URL("file://"+listado[i].getAbsolutePath());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }           
    System.out.println("Files:"+listado.length+"; Time 2: "+(System.currentTimeMillis()-t0)+" ms");

My output is:

Files:14; Time 1: 1985 ms
Files:14; Time 2: 516 ms
like image 33
Tomas Narros Avatar answered Sep 28 '22 07:09

Tomas Narros