java.nio.file.Files.walkFileTree()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(350)

本文整理了Java中java.nio.file.Files.walkFileTree()方法的一些代码示例,展示了Files.walkFileTree()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.walkFileTree()方法的具体详情如下:
包路径:java.nio.file.Files
类名称:Files
方法名:walkFileTree

Files.walkFileTree介绍

暂无

代码示例

代码示例来源:origin: apache/kafka

/**
 * Recursively delete the given file/directory and any subfiles (if any exist)
 *
 * @param file The root file at which to begin deleting
 */
public static void delete(final File file) throws IOException {
  if (file == null)
    return;
  Files.walkFileTree(file.toPath(), new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFileFailed(Path path, IOException exc) throws IOException {
      // If the root path did not exist, ignore the error; otherwise throw it.
      if (exc instanceof NoSuchFileException && path.toFile().equals(file))
        return FileVisitResult.TERMINATE;
      throw exc;
    }
    @Override
    public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
      Files.delete(path);
      return FileVisitResult.CONTINUE;
    }
    @Override
    public FileVisitResult postVisitDirectory(Path path, IOException exc) throws IOException {
      Files.delete(path);
      return FileVisitResult.CONTINUE;
    }
  });
}

代码示例来源:origin: Alluxio/alluxio

/**
 * Deletes a file or a directory, recursively if it is a directory.
 *
 * @param path pathname to be deleted
 */
public static void deletePathRecursively(String path) throws IOException {
 Path root = Paths.get(path);
 Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
  @Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
   Files.delete(file);
   return FileVisitResult.CONTINUE;
  }
  @Override
  public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
   if (e == null) {
    Files.delete(dir);
    return FileVisitResult.CONTINUE;
   } else {
    throw e;
   }
  }
 });
}

代码示例来源:origin: spotbugs/spotbugs

Path tempJar = File.createTempFile("SpotBugsAnalysisRunner", ".jar").toPath();
try (OutputStream output = Files.newOutputStream(tempJar, StandardOpenOption.WRITE);
    JarOutputStream jar = new JarOutputStream(output)) {
  Path resourceRoot = Paths.get(uri).getParent();
  Files.walkFileTree(resourceRoot, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

代码示例来源:origin: ethereum/ethereumj

public static List<String> recursiveList(String path) throws IOException {
  final List<String> files = new ArrayList<>();
  Files.walkFileTree(Paths.get(path), new FileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
      return FileVisitResult.CONTINUE;
    }
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
      files.add(file.toString());
      return FileVisitResult.CONTINUE;
    }
    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
      return FileVisitResult.CONTINUE;
    }
    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
      return FileVisitResult.CONTINUE;
    }
  });
  return files;
}

代码示例来源:origin: Tencent/tinker

public ArrayList<String> getDeletedResource(File oldApkDir, File newApkDir) throws IOException {
  //get deleted resource
  DeletedResVisitor deletedResVisitor = new DeletedResVisitor(config, newApkDir.toPath(), oldApkDir.toPath());
  Files.walkFileTree(oldApkDir.toPath(), deletedResVisitor);
  return deletedResVisitor.deletedFiles;
}

代码示例来源:origin: bytedeco/javacpp

List<String> paths =  p.get("platform.resourcepath");
Path directoryPath = directory.toPath();
for (String resource : resources) {
  final Path target = directoryPath.resolve(resource);
    final Path source = Paths.get(path, resource);
    if (Files.exists(source)) {
      logger.info("Copying " + source);
      Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
        @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
          Path targetdir = target.resolve(source.relativize(dir));

代码示例来源:origin: libgdx/packr

/**
 * Copies directories, preserving file attributes.
 * <p>
 * The {@link org.zeroturnaround.zip.commons.FileUtilsV2_2#copyDirectory(File, File)} function does not
 * preserve file attributes.
 */
static void copyDirectory(File sourceDirectory, File targetDirectory) throws IOException {
  final Path sourcePath = Paths.get(sourceDirectory.toURI()).toRealPath();
  final Path targetPath = Paths.get(targetDirectory.toURI());
  Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
      Path relative = sourcePath.relativize(dir);
      Path target = targetPath.resolve(relative);
      File folder = target.toFile();
      if (!folder.exists()) {
        mkdirs(folder);
      }
      return FileVisitResult.CONTINUE;
    }
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
      Path relative = sourcePath.relativize(file);
      Path target = targetPath.resolve(relative);
      Files.copy(file, target, StandardCopyOption.COPY_ATTRIBUTES);
      return FileVisitResult.CONTINUE;
    }
  });
}

代码示例来源:origin: typ0520/fastdex

protected void walkFileTree(File directory, final ScanFilter scanFilter) throws IOException {
  Files.walkFileTree(directory.toPath(),new SimpleFileVisitor<Path>(){
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
      return BaseDirectorySnapshoot.this.visitFile(file,attrs,scanFilter);
    }
  });
}

代码示例来源:origin: mulesoft/mule

/**
 * Traverses the directory tree from the {@value #MAVEN_MULTI_MODULE_PROJECT_DIRECTORY} property to look for Maven projects that
 * are also listed as entries in class path.
 *
 * @param rootProjectDirectoryProperty the root directory of the multi-module project build session
 * @param classPath the whole class path built by IDE or Maven (surefire Maven plugin)
 * @param rootArtifactClassesFolder the current rootArtifact directory
 */
private void discoverMavenReactorProjects(String rootProjectDirectoryProperty, List<URL> classPath,
                     File rootArtifactClassesFolder) {
 Path rootProjectDirectory = get(rootProjectDirectoryProperty);
 logger.debug("Defined rootProjectDirectory='{}'", rootProjectDirectory);
 File currentDir = rootArtifactClassesFolder;
 File lastMavenProjectDir = currentDir;
 while (containsMavenProject(currentDir) && !currentDir.toPath().equals(rootProjectDirectory.getParent())) {
  lastMavenProjectDir = currentDir;
  currentDir = currentDir.getParentFile();
 }
 logger.debug("Top folder found, parent pom found at: '{}'", lastMavenProjectDir);
 try {
  walkFileTree(lastMavenProjectDir.toPath(), new MavenDiscovererFileVisitor(classPath));
 } catch (IOException e) {
  throw new RuntimeException("Error while discovering Maven projects from path: " + currentDir.toPath());
 }
}

代码示例来源:origin: syncany/syncany

private ChangeSet findLocalChangedAndNewFiles(final File root, Map<String, FileVersion> filesInDatabase)
    throws FileNotFoundException, IOException {
  Path rootPath = Paths.get(root.getAbsolutePath());
  StatusFileVisitor fileVisitor = new StatusFileVisitor(rootPath, filesInDatabase);
  Files.walkFileTree(rootPath, fileVisitor);
  return fileVisitor.getChangeSet();
}

代码示例来源:origin: apache/storm

protected void delete(File path) throws IOException {
  if (Files.exists(path.toPath())) {
    Files.walkFileTree(path.toPath(), new SimpleFileVisitor<Path>() {
      @Override
      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
      }
      @Override
      public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        Files.delete(dir);
        return FileVisitResult.CONTINUE;
      }
    });
  }
}

代码示例来源:origin: deeplearning4j/nd4j

URI uri = u.toURI();
try (FileSystem fileSystem = (uri.getScheme().equals("jar") ? FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap()) : null)) {
  Path myPath = Paths.get(uri);
  Files.walkFileTree(myPath, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {

代码示例来源:origin: SonarSource/sonarqube

public void clean() {
 try {
  if (tempDir.exists()) {
   Files.walkFileTree(tempDir.toPath(), DeleteRecursivelyFileVisitor.INSTANCE);
  }
 } catch (IOException e) {
  LOG.error("Failed to delete temp folder", e);
 }
}

代码示例来源:origin: networknt/light-4j

throws IOException{
final Path destDir = Paths.get(destDirname);
  Files.walkFileTree(root, new SimpleFileVisitor<Path>(){
    @Override
    public FileVisitResult visitFile(Path file,

代码示例来源:origin: embulk/embulk

private void deleteFilesIfExistsRecursively(File dir) throws IOException {
    Files.walkFileTree(dir.toPath(), new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
          try {
            Files.deleteIfExists(file);
          } catch (IOException ex) {
            // ignore IOException
          }
          return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
          try {
            Files.deleteIfExists(dir);
          } catch (IOException ex) {
            // ignore IOException
          }
          return FileVisitResult.CONTINUE;
        }
      });
  }
}

代码示例来源:origin: guoguibing/librec

Files.walkFileTree(Paths.get(inputDataPath), finder);
long allFileSize = 0;
for (Long everyFileSize : fileSizeList) {

代码示例来源:origin: apache/flume

private Set<Path> getTrackerDirCompletedFiles() throws IOException {
 final Set<Path> completedFiles = new HashSet<>();
 if (TrackingPolicy.TRACKER_DIR != trackingPolicy) {
  return completedFiles;
 }
 Path trackerDirPath = trackerDirectory.toPath();
 Files.walkFileTree(trackerDirPath, new SimpleFileVisitor<Path>() {
  @Override
  public FileVisitResult visitFile(Path candidate, BasicFileAttributes attrs)
      throws IOException {
   String fileName = candidate.getFileName().toString();
   if (fileName.endsWith(completedSuffix)) {
    completedFiles.add(candidate.toAbsolutePath());
   }
   return FileVisitResult.CONTINUE;
  }
 });
 return completedFiles;
}

代码示例来源:origin: networknt/light-4j

final Path src = Paths.get(filename);
  Files.walkFileTree(src, new SimpleFileVisitor<Path>(){
    @Override
    public FileVisitResult visitFile(Path file,

代码示例来源:origin: google/guava

@Override
public void tearDown() throws IOException {
 // delete rootDir and its contents
 java.nio.file.Files.walkFileTree(
   rootDir.toPath(),
   new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
      throws IOException {
     java.nio.file.Files.deleteIfExists(file);
     return FileVisitResult.CONTINUE;
    }
    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
     if (exc != null) {
      return FileVisitResult.TERMINATE;
     }
     java.nio.file.Files.deleteIfExists(dir);
     return FileVisitResult.CONTINUE;
    }
   });
}

代码示例来源:origin: atomix/atomix

Path directory = Paths.get("target/perf-logs/");
if (Files.exists(directory)) {
 Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
  @Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

相关文章

微信公众号

最新文章

更多