3

I want to make my program print huge list of all files that I have on my computer. My problem is that it only prints files from first folder of the first hard-drive, when I want it to print all files located on my computer. Any ideas what am I doing wrong here? Thanks.

Here is code I use:

Main:

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        ArrayList<File> roots = new ArrayList();
        roots.addAll(Arrays.asList(File.listRoots()));


        for (File file : roots) {
            new Searcher(file.toString().replace('\\', '/')).search();
        }
    }
}

and Searcher class:

import java.io.File;

public class Searcher {

    private String root;

    public Searcher(String root) {
        this.root = root;
    }

    public void search() {
        System.out.println(root);
        File folder = new File(root);
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) {
            String path = file.getPath().replace('\\', '/');
            System.out.println(path);
            if (!path.contains(".")) {
                new Searcher(path + "/").search();
            }
        }
    }
}
Rohit Malish
  • 3,209
  • 12
  • 49
  • 67
  • Although there are duplicate questions (http://stackoverflow.com/questions/3154488/best-way-to-iterate-through-a-directory-in-java and http://stackoverflow.com/questions/4917326/how-to-iterate-over-the-files-of-a-certain-directory-in-java), some answers are including Java 7, which was not available when the others were posted. – rajah9 May 21 '12 at 19:51

5 Answers5

6

I just tried this and it worked for me. I did have to add one null check and changed the directory evaluation method though:

package test;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

public class Searcher {
    public static void main(String[] args) {
        ArrayList<File> roots = new ArrayList<File>();
        roots.addAll(Arrays.asList(File.listRoots()));


        for (File file : roots) {
            new Searcher(file.toString().replace('\\', '/')).search();
        }
    }

    private String root;

    public Searcher(String root) {
        this.root = root;
    }

    public void search() {
        System.out.println(root);
        File folder = new File(root);
        File[] listOfFiles = folder.listFiles();
        if(listOfFiles == null) return;  // Added condition check
        for (File file : listOfFiles) {
            String path = file.getPath().replace('\\', '/');
            System.out.println(path);
            if (file.isDirectory()) {
                new Searcher(path + "/").search();
            }
        }
    }
}
mprivat
  • 21,582
  • 4
  • 54
  • 64
2

You should update your search method like this:

public void search() {
        System.out.println(root);
        File folder = new File(root);
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) {
            String path = file.getPath().replace('\\', '/');
            System.out.println(path);
            if (file.isDirectory()) {
                new Searcher(path + "/").search();
            }
        }
    }
Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
1

If Java 7 is an option, look into the walkFileTree() method. It will allow you to visit all files and directories in a tree, which you can start from the root of your drive. Just implement a basic FileVisitor to process the file attributes for each Path. You can get started here.

Thorn G
  • 12,620
  • 2
  • 44
  • 56
  • you might want to update your first hyperlink... There is no walkFileTree() method on the Files page. And thank you for providing this Java 7 option. – rajah9 May 21 '12 at 19:45
0

I don't know what error you are getting but I got a NPE because you are not checking for the null after the following line.

File[] listOfFiles = folder.listFiles();

After changing the code as follows it seemed to run fine , I stopped it because I have a lot of files. I am assuming it will go on to the next root after the first root(c:/ in my case)

import java.io.File;

import java.util.ArrayList; import java.util.Arrays;

public class Search {

    public static void main(String[] args) {
        ArrayList<File> roots = new ArrayList();
        roots.addAll(Arrays.asList(File.listRoots()));


        for (File file : roots) {
            System.out.println(file.toString());
            new Searcher(file.toString().replace('\\', '/')).search();
        }
    }
}

class Searcher {

private String root;

public Searcher(String root) {
    this.root = root;
}

public void search() {
    System.out.println(root);
    File folder = new File(root);
    File[] listOfFiles = folder.listFiles();
    if(listOfFiles!=null)
    {
    for (File file : listOfFiles) {
        String path = file.getPath().replace('\\', '/');
        System.out.println(path);
        if (!path.contains(".")) {
            new Searcher(path + "/").search();
        }
    }
    }
}

}

SantoshK
  • 96
  • 4