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(); } } } } 
1

5 Answers 5

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(); } } } } 
Sign up to request clarification or add additional context in comments.

Comments

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(); } } } 

Comments

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.

1 Comment

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.
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(); } } } } 

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.