0

So I have to use the Java file tree system because .listfiles files is for some reason incredibly slow going through a remote network. However all the Java file tree system examples list all the files in the subdirectories, severely slowing down the program. How can I make it so that it will only search the directory and return the names of the folders and files only within that directory and not the subdirectories.

Sample Code:

package javaapplication6;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

/** Recursive listing with SimpleFileVisitor in JDK 7. */
public final class JavaApplication6 {

  public static void main(String... aArgs) throws IOException{
    String ROOT = "\\\\directory";
    FileVisitor<Path> fileProcessor = new ProcessFile();
    Files.walkFileTree(Paths.get(ROOT), fileProcessor);
  }

  private static final class ProcessFile extends SimpleFileVisitor<Path> {
    @Override public FileVisitResult visitFile(
      Path aFile, BasicFileAttributes aAttrs
    ) throws IOException {
      System.out.println("Processing file:" + aFile);
      return FileVisitResult.CONTINUE;
    }

    @Override  public FileVisitResult preVisitDirectory(
      Path aDir, BasicFileAttributes aAttrs
    ) throws IOException {
      System.out.println("Processing directory:" + aDir);
      return FileVisitResult.CONTINUE;
    }
  }
} 

Any insight or help would be greatly appreciated, thanks.

Brandan B
  • 464
  • 2
  • 6
  • 21
  • If you don't want to recurse through the files, why are you using `Files.walkFileTree`? You can just use a `DirectoryStream` with a filter. – Tunaki Oct 17 '15 at 15:36
  • Isn't walkfiletree faster? – Brandan B Oct 17 '15 at 15:41
  • It's not a question of speed but of purpose: `walkFileTree` is desiged, as per its name, to walk a file tree, this means to recurse inside subdirectories. If you just want to list the contents of a folder (with no recursion) a `DirectoryStream` is what you should use. This class is designed to handle very very big directories. – Tunaki Oct 17 '15 at 15:43
  • Are you sure? because here: https://bugs.openjdk.java.net/browse/JDK-8008469 the suggested solution was to use walkfiletree – Brandan B Oct 17 '15 at 15:45
  • Reading [this comment](https://bugs.openjdk.java.net/browse/JDK-8008469?focusedCommentId=13421036&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13421036) from the bug you link to suggests that it is trickier than this. The performance issue seems to be around `Files.isDirectory` but if you are just listing the entry of a directory using `DirectoryStream` you won't need to call this method. I suggest you try this solution and see if you hit any performance issue. – Tunaki Oct 17 '15 at 15:49

2 Answers2

0

Using Directory stream seems to work more quickly and easily.

Brandan B
  • 464
  • 2
  • 6
  • 21
0

Use the longer version of the walkFileTree method that allows you to set maxDepth like so:

Files.walkFileTree(Paths.get(ROOT), EnumSet.noneOf(FileVisitOption.class),
   1, fileProcessor);

Note that unlike the simpler case sub-directories of ROOT will generate calls to visitFile. More generally sub-directories at the maxDepth level generate calls to visitFile but not calls to preVisitDirectory and postVisitDirectory.

FinnTheHuman
  • 135
  • 2
  • 9