简体   繁体   English

使用Java的文件树系统仅获取文件和文件夹,而不获取子目录

[英]Using Java's file tree system to get only files and folders not subdirectories

So I have to use the Java file tree system because .listfiles files is for some reason incredibly slow going through a remote network. 因此,我必须使用Java文件树系统,因为由于某种原因,.listfiles文件通过远程网络的运行速度非常慢。 However all the Java file tree system examples list all the files in the subdirectories, severely slowing down the program. 但是,所有Java文件树系统示例均列出了子目录中的所有文件,从而严重降低了程序速度。 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. 感谢您的任何见解或帮助。

使用目录流似乎可以更快,更轻松地工作。

Use the longer version of the walkFileTree method that allows you to set maxDepth like so: 使用walkFileTree方法的较长版本,该方法允许您像这样设置maxDepth

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 . 请注意,与简单情况不同,ROOT的子目录将生成对visitFile调用。 More generally sub-directories at the maxDepth level generate calls to visitFile but not calls to preVisitDirectory and postVisitDirectory . 通常,在maxDepth级别的子目录生成对visitFile调用,但不生成对preVisitDirectorypostVisitDirectory调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java的目录流仅在目录中而不是其他子目录中获取文件/子目录 - How to use Java's directory stream to get files/subdirectories only within directory not other subdirectories Java,仅列出目录中的子目录,而不是文件 - Java, List only subdirectories from a directory, not files Java“文件”和“文件”仅用于本地系统吗? - Is Java “File” and “Files” only meant for local system? Java-递归获取目录和子目录中的所有文件 - Java - Get all files in directories and subdirectories recursively 如何使用Java仅保存zip文件并删除其他文件夹? - How to save only zip files and remove other folders using java? 使用Java在Linux中列出子目录/文件 - listing subdirectories/files in linux using java 如何使用 Java/Spring 自动管理文件系统目录树? - How to automatically manage a file system directory tree using Java/Spring? 如何使用Java获取目录及其子目录中多个文件的相同xml元素值? - How to get the same xml element value of multiple files in a directories and its subdirectories using java? aws s3列出的文件夹和文件仅适用于顶级(第一)Java - aws s3 listing folders and files won't work only for top (first) level Java Java:如何仅获取在特定级别存在的子目录名称? - Java: How to get only subdirectories name present at certain level?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM