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.
Using Directory stream seems to work more quickly and easily.