title: Iterate through all files in Java
body: <p>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.</p>
  
  <p>Here is code I use:</p>
  
  <p>Main:</p>
  
  <pre><code>import java.io.File;
  import java.util.ArrayList;
  import java.util.Arrays;
  
  public class Main {
  
      public static void main(String[] args) {
          ArrayList&lt;File&gt; roots = new ArrayList();
          roots.addAll(Arrays.asList(File.listRoots()));
  
  
          for (File file : roots) {
              new Searcher(file.toString().replace('\\', '/')).search();
          }
      }
  }
  </code></pre>
  
  <p>and Searcher class:</p>
  
  <pre><code>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();
              }
          }
      }
  }
  </code></pre>