Java 类java.nio.file.SimpleFileVisitor 实例源码

项目:Elasticsearch    文件:LogConfigurator.java   
static void resolveConfig(Environment env, final Settings.Builder settingsBuilder) {

        try {
            Files.walkFileTree(env.configFile(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String fileName = file.getFileName().toString();
                    if (fileName.startsWith("logging.")) {
                        for (String allowedSuffix : ALLOWED_SUFFIXES) {
                            if (fileName.endsWith(allowedSuffix)) {
                                loadConfig(file, settingsBuilder);
                                break;
                            }
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException ioe) {
            throw new ElasticsearchException("Failed to load logging configuration", ioe);
        }
    }
项目:Squid    文件:FileUtilities.java   
/**
 *
 * @param directory
 * @throws IOException
 */
public static void recursiveDelete(Path directory) throws IOException {
    Files.walkFileTree(directory, 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;
        }
    });
}
项目:HardVox    文件:ChunkDataStore.java   
@Override
public void close() {
    memory.clear();
    try {
        Files.walkFileTree(tmpDir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.deleteIfExists(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.deleteIfExists(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
项目:launcher-backend    文件:Paths.java   
/**
 * Deletes a directory recursively
 *
 * @param directory
 * @throws IOException
 */
public static void deleteDirectory(Path directory) throws IOException {
    Files.walkFileTree(directory, 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;
        }
    });
}
项目:launcher-backend    文件:FileUploadHelper.java   
/**
 * Deletes a directory recursively
 *
 * @param directory
 * @return true if deletion succeeds, false otherwise
 */
public static boolean deleteDirectory(Path directory) {
    if (directory != null) {
        try {
            Files.walkFileTree(directory, 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;
                }
            });
        } catch (IOException ignored) {
            return false;
        }
    }
    return true;
}
项目:sumo    文件:ClearCacheAction.java   
@Override
public boolean execute() {
    File folder=CacheManager.getRootCacheInstance().getPath();
    //System.gc();
    try {
        Files.walkFileTree(folder.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;
               }

           });
    } catch (IOException e) {
        logger.error(e.getMessage());
        return false;
    }
    super.notifyEvent(new SumoActionEvent(SumoActionEvent.ENDACTION,"cache cleaned...", -1));
    return true;
}
项目:elasticsearch_my    文件:FileUtils.java   
private static void collectFiles(final Path dir, final String fileSuffix, final Map<String, Set<Path>> files) throws IOException {
    Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (file.toString().endsWith(fileSuffix)) {
                String groupName = dir.relativize(file.getParent()).toString();
                Set<Path> filesSet = files.get(groupName);
                if (filesSet == null) {
                    filesSet = new HashSet<>();
                    files.put(groupName, filesSet);
                }
                filesSet.add(file);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
项目:elasticsearch_my    文件:ESLoggerUsageChecker.java   
private static void checkLoggerUsage(Consumer<WrongLoggerUsage> wrongUsageCallback, String... classDirectories)
    throws IOException {
    for (String classDirectory : classDirectories) {
        Path root = Paths.get(classDirectory);
        if (Files.isDirectory(root) == false) {
            throw new IllegalArgumentException(root + " should be an existing directory");
        }
        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (Files.isRegularFile(file) && file.getFileName().toString().endsWith(".class")) {
                    try (InputStream in = Files.newInputStream(file)) {
                        ESLoggerUsageChecker.check(wrongUsageCallback, in);
                    }
                }
                return super.visitFile(file, attrs);
            }
        });
    }
}
项目:elasticsearch_my    文件:FsBlobContainer.java   
@Override
public void deleteBlob(String blobName) throws IOException {
    Path blobPath = path.resolve(blobName);
    if (Files.isDirectory(blobPath)) {
        // delete directory recursively as long as it is empty (only contains empty directories),
        // which is the reason we aren't deleting any files, only the directories on the post-visit
        Files.walkFileTree(blobPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    } else {
        Files.delete(blobPath);
    }
}
项目:BIMplatform    文件:PathUtils.java   
public static void removeDirectoryWithContent(Path directory) throws IOException {
    if (Files.isDirectory(directory)) {
        Files.walkFileTree(directory, 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;
            }
        });
    }
}
项目:VKMusicUploader    文件:WatchDirs.java   
/**
 * Starts watching the subdirectories of the provided directory for changes.
 * @param root Root directory of the subdirectories to register.
 * @throws IOException If a subdirectory cannot be registered.
 * @checkstyle RequireThisCheck (20 lines)
 * @checkstyle NonStaticMethodCheck (20 lines)
 */
private void processSubevents(final Path root) throws IOException {
    try {
        Files.walkFileTree(
            root,
            new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(
                    final Path subdir,
                    final BasicFileAttributes attrs
                ) throws IOException {
                    registerDirectory(subdir);
                    return FileVisitResult.CONTINUE;
                }
            });
    } catch (final IOException ex) {
        throw new IOException("Failed to register subdirectories", ex);
    }
}
项目:Java-Data-Science-Cookbook    文件:IndexFiles.java   
static void indexDocs(final IndexWriter writer, Path path) throws IOException {
    if (Files.isDirectory(path)) {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                try {
                    indexDoc(writer, file, attrs.lastModifiedTime().toMillis());
                } catch (IOException ignore) {
                }
                return FileVisitResult.CONTINUE;
            }
        }
                );
    } else {
        indexDoc(writer, path, Files.getLastModifiedTime(path).toMillis());
    }
}
项目:hippo-groovy-updater    文件:FileSystemWatcher.java   
private void registerRecursively(final Path directory) throws IOException {
    Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult preVisitDirectory(final Path visitedDirectory, final BasicFileAttributes attrs) throws IOException {
            if (!FileSystemWatcher.this.watchedFiles.matchesDirectory(visitedDirectory)) {
                return FileVisitResult.SKIP_SUBTREE;
            }


            final WatchKey key = visitedDirectory.register(watcher,
                    ENTRY_CREATE,
                    ENTRY_MODIFY,
                    ENTRY_DELETE);

            watchedPaths.put(key, visitedDirectory);

            return FileVisitResult.CONTINUE;
        }
    });
}
项目:openjdk-systemtest    文件:WalkFileTreeTest.java   
public void testWalkFileTreeMultiple() throws IOException {     
    final int ITERATIONS = 1000;
    Path newDirectory = WALKFILETREE_FILE_DIR;  
    for (int counter=0; counter< ITERATIONS; counter++) {       
        HangNotifier.ping();    
        final List<Path> visitorFiles = new LinkedList<Path>();     
        // Check that we keep returning the same set of files!
        Files.walkFileTree(newDirectory, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) {
                if (path.toString().endsWith(".txt")) {
                    visitorFiles.add(path);
                }
                return FileVisitResult.CONTINUE;
            }
        });

        if(!ReiserSpotter.getIsReiser()){
            assertEquals("Wrong number of files walked on iteration " + counter, NUMBER_OF_FILES, visitorFiles.size());
        }       
    }   
}
项目:guava-mock    文件:MoreFilesTest.java   
@Override
protected void tearDown() throws Exception {
  if (tempDir != null) {
    // delete tempDir and its contents
    Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {
      @Override
      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.deleteIfExists(file);
        return FileVisitResult.CONTINUE;
      }

      @Override
      public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        if (exc != null) {
          return FileVisitResult.TERMINATE;
        }
        Files.deleteIfExists(dir);
        return FileVisitResult.CONTINUE;
      }
    });
  }
}
项目:ProjectAres    文件:MapSource.java   
public Set<Path> getMapFolders(final Logger logger) throws IOException {
    final Set<Path> mapFolders = new HashSet<>();
    for(Path root : getRootPaths()) {
        int depth = "".equals(root.toString()) ? 0 : Iterables.size(root);
        Files.walkFileTree(getPath().resolve(root), ImmutableSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth - depth, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                if(!isExcluded(dir)) {
                    if(MapFolder.isMapFolder(dir)) {
                        mapFolders.add(dir);
                    }
                    return FileVisitResult.CONTINUE;
                } else {
                    logger.fine("Skipping excluded path " + dir);
                    return FileVisitResult.SKIP_SUBTREE;
                }
            }
        });
    }
    return mapFolders;
}
项目:omero-ms-queue    文件:ClassPathFactory.java   
/**
 * Builds a class path with all the files selected by the given predicate 
 * in the specified base directory and, recursively, in all of its 
 * sub-directories.
 * @param dir the directory in which to find the (jar) files.
 * @param select tests whether or not to include a regular file.
 * @return the class path.
 * @throws NullPointerException if the argument is {@code null}.
 * @throws IOException if an I/O error occurs while trawling the directory.
 */
public static ClassPath fromDir(Path dir, Predicate<Path> select) 
        throws IOException {
    requireNonNull(dir, "dir");
    requireNonNull(select, "select");

    ClassPath cp = new ClassPath();

    Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file,
                BasicFileAttributes attrs) throws IOException {
            if (attrs.isRegularFile() && select.test(file)) {
                cp.add(file);
            }
            return FileVisitResult.CONTINUE;
        }
    });

    return cp;
}
项目:kowalski    文件:ApplicationTest.java   
private void deleteDirectory(Path directory) throws IOException {
    Files.walkFileTree(directory, 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;
        }

    });
}
项目:Elasticsearch    文件:LocalTranslog.java   
public LocalTranslog(TranslogConfig config) throws IOException {
    super(config.getShardId(), config.getIndexSettings());
    ReadWriteLock rwl = new ReentrantReadWriteLock();
    readLock = new ReleasableLock(rwl.readLock());
    writeLock = new ReleasableLock(rwl.writeLock());
    this.translogPath = config.getTranslogPath();
    // clean all files
    Files.createDirectories(this.translogPath);
    Files.walkFileTree(this.translogPath, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file,
                BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }
    });

    // create a new directory
    writeChannel = FileChannel.open(this.translogPath.resolve(getFileNameFromId(tmpTranslogGeneration.get())), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
    writtenOffset = 0;
}
项目:jtsgen    文件:TsGenProcessorTmpDirTest.java   
@After
public void deleteTmpDir() throws IOException {
    Files.walkFileTree(this.tmpDir, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult postVisitDirectory(final Path theDir, final IOException e) throws IOException {
            if (e != null) return TERMINATE;
            Files.delete(theDir);
            return CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(final Path theFile, final BasicFileAttributes attrs) throws IOException {
            Files.delete(theFile);
            return CONTINUE;
        }
    });
}
项目:Lagerta    文件:FileUtils.java   
/**
 * Deletes directory along with all inner files.
 *
 * @param path to directory.
 * @throws IOException if there are errors during deleting.
 */
public static void deleteDir(Path path) throws IOException {
    if (!Files.exists(path)) {
        return;
    }
    Files.walkFileTree(path, 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 super.postVisitDirectory(dir, exc);
        }
    });
}
项目:Lagerta    文件:ExportTest.java   
/** */
@Test(expected = IgniteException.class)
public void failIfChecksumDoesNotMatch() throws IOException {
    URI exportDirUri = folder.newFolder().toURI();
    String destination = exportDirUri.toString();
    backupInCompute(destination);

    Files.walkFileTree(Paths.get(exportDirUri), new SimpleFileVisitor<Path>() {
        @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (FileChecksumHelper.isChecksum(file)) {
                Files.write(file, "corrupted".getBytes());
            }
            return FileVisitResult.CONTINUE;
        }
    });
    restoreInCompute(destination);
}
项目:Lagerta    文件:ExportTest.java   
/** */
private void corruptDataFile(URI exportDirUri) throws IOException {
    Files.walkFileTree(Paths.get(exportDirUri), new SimpleFileVisitor<Path>() {

        private boolean firstFile;

        @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String fileName = file.getFileName().toString();
            if (!fileName.equals(ExportNameConventionUtil.METADATA_RESOURCE_NAME)) {
                // skip first data file
                if (!firstFile) {
                    firstFile = true;
                    return FileVisitResult.CONTINUE;
                }
                Files.write(file, "corruption".getBytes());
                return FileVisitResult.TERMINATE;
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
项目:Lagerta    文件:FileUtils.java   
/**
 * Deletes directory along with all inner files.
 *
 * @param path to directory.
 * @throws IOException if there are errors during deleting.
 */
public static void deleteDir(Path path) throws IOException {
    if (!Files.exists(path)) {
        return;
    }
    Files.walkFileTree(path, 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 super.postVisitDirectory(dir, exc);
        }
    });
}
项目:bazel-tools    文件:PathUtils.java   
public static void removeRecursive(final Path path) throws IOException {
  Files.walkFileTree(
      path,
      new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
            throws IOException {
          Files.deleteIfExists(file);
          return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)
            throws IOException {
          Files.deleteIfExists(dir);
          return FileVisitResult.CONTINUE;
        }
      });
}
项目:jpa2ddl    文件:FileResolver.java   
static List<String> listClassNamesInPackage(String packageName) throws Exception {
    List<String> classes = new ArrayList<>();
    Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packageName.replace('.', File.separatorChar));
    if (!resources.hasMoreElements()) {
        throw new IllegalStateException("No package found: " + packageName);
    }
    PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:*.class");
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        Files.walkFileTree(Paths.get(resource.toURI()), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                if (pathMatcher.matches(path.getFileName())) {
                    try {
                        String className = Paths.get(resource.toURI()).relativize(path).toString().replace(File.separatorChar, '.');
                        classes.add(packageName + '.' + className.substring(0, className.length() - 6));
                    } catch (URISyntaxException e) {
                        throw new IllegalStateException(e);
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
    return classes;
}
项目:openjdk-jdk10    文件:JavacFileManager.java   
public ArchiveContainer(Path archivePath) throws IOException, ProviderNotFoundException, SecurityException {
    this.archivePath = archivePath;
    if (multiReleaseValue != null && archivePath.toString().endsWith(".jar")) {
        Map<String,String> env = Collections.singletonMap("multi-release", multiReleaseValue);
        FileSystemProvider jarFSProvider = fsInfo.getJarFSProvider();
        Assert.checkNonNull(jarFSProvider, "should have been caught before!");
        this.fileSystem = jarFSProvider.newFileSystem(archivePath, env);
    } else {
        this.fileSystem = FileSystems.newFileSystem(archivePath, null);
    }
    packages = new HashMap<>();
    for (Path root : fileSystem.getRootDirectories()) {
        Files.walkFileTree(root, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE,
                new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                        if (isValid(dir.getFileName())) {
                            packages.put(new RelativeDirectory(root.relativize(dir).toString()), dir);
                            return FileVisitResult.CONTINUE;
                        } else {
                            return FileVisitResult.SKIP_SUBTREE;
                        }
                    }
                });
    }
}
项目:openjdk-jdk10    文件:SourcePathTest.java   
/**
 * This method is primarily used to give visual confirmation that a test case
 * generated files when the compilation succeeds and so generates no other output,
 * such as error messages.
 */
List<Path> showFiles(Path dir) throws IOException {
    List<Path> files = new ArrayList<>();
    Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
            if (Files.isRegularFile(file)) {
                out.println("Found " + file);
                files.add(file);
            }
            return FileVisitResult.CONTINUE;
        }
    });
    return files;
}
项目:openjdk-jdk10    文件:ToolBox.java   
/**
 * Deletes all content of a directory (but not the directory itself).
 * @param root the directory to be cleaned
 * @throws IOException if an error occurs while cleaning the directory
 */
public void cleanDirectory(Path root) throws IOException {
    if (!Files.isDirectory(root)) {
        throw new IOException(root + " is not a directory");
    }
    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes a) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
            if (e != null) {
                throw e;
            }
            if (!dir.equals(root)) {
                Files.delete(dir);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
项目:openjdk-jdk10    文件:ToolBox.java   
/**
 * Find files matching the file extension, in one or more directories.
 * <p>Similar to the shell "find" command: {@code find paths -name \*.ext}.
 * @param fileExtension the extension to search for
 * @param paths the directories in which to search for files
 * @return the files matching the file extension
 * @throws IOException if an error occurred while searching for files
 */
public Path[] findFiles(String fileExtension, Path... paths) throws IOException {
    Set<Path> files = new TreeSet<>();  // use TreeSet to force a consistent order
    for (Path p : paths) {
        Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                    throws IOException {
                if (file.getFileName().toString().endsWith(fileExtension)) {
                    files.add(file);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
    return files.toArray(new Path[files.size()]);
}
项目:openjdk-jdk10    文件:ClasspathDependencies.java   
static void delete(Path root) throws IOException {
    if (!Files.exists(root))
        return;
    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
             @Override
             public FileVisitResult visitFile(Path f, BasicFileAttributes a)
                     throws IOException {
                 Files.delete(f);
                 return FileVisitResult.CONTINUE;
             }

             @Override
             public FileVisitResult postVisitDirectory(Path dir, IOException e)
                     throws IOException {
                 if (e != null)
                     throw e;
                 if (!dir.equals(root))
                     Files.delete(dir);
                 return FileVisitResult.CONTINUE;
             }
        });
}
项目:imagingbook-common    文件:DirectoryWalker.java   
/**
 * Traverses the directory tree and collects all matching file names.
 * TODO: clean up exception handling.
 * 
 * @param startDir start directory
 * @throws IOException
 */
private void traverse(Path startDir) throws IOException {
    Files.walkFileTree(startDir, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String pathName = file.toString();
            if (hasMatchingExtension(pathName)) {
                fileList.add(pathName);
                // System.out.println("added file " + file.toString());
            }
            return FileVisitResult.CONTINUE;
        }
        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
            if (e == null) {
                //System.out.println("visiting " + dir.toString());
                return FileVisitResult.CONTINUE;
            } else {
                // directory iteration failed
                throw e;
            }
        }
    });
}
项目:Hollow-Knight-SaveManager    文件:Listeners.java   
public static void deleteFiles (String path){    
     try {
         Path directory = Paths.get(path);
         Files.walkFileTree(directory, 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;
            }
         });
    } catch (IOException e) {
        e.printStackTrace();
    }      
}
项目:googles-monorepo-demo    文件:MoreFilesTest.java   
@Override
protected void tearDown() throws Exception {
  if (tempDir != null) {
    // delete tempDir and its contents
    Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {
      @Override
      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.deleteIfExists(file);
        return FileVisitResult.CONTINUE;
      }

      @Override
      public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        if (exc != null) {
          return FileVisitResult.TERMINATE;
        }
        Files.deleteIfExists(dir);
        return FileVisitResult.CONTINUE;
      }
    });
  }
}
项目:oryx2    文件:IOUtils.java   
/**
 * Deletes the given path, and if it is a directory, all files and subdirectories within it.
 *
 * @param rootDir directory to delete
 * @throws IOException if any error occurs while deleting files or directories
 */
public static void deleteRecursively(Path rootDir) throws IOException {
  if (rootDir == null || !Files.exists(rootDir)) {
    return;
  }
  Files.walkFileTree(rootDir, 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;
    }
  });
}
项目:MinecraftServerSync    文件:ServerSynchronizer.java   
private static void deleteDirectory(Path dirToDelete) {
    try {
        Files.walkFileTree(dirToDelete, 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;
            }
        });
    } catch (IOException e) {
        LOGGER.warn("Could not delete directory {}", dirToDelete);
        LogUtil.stacktrace(LOGGER, e);
    }
}
项目:dsync-client    文件:WatcherRegisterConsumer.java   
/**
     * Register watchers recursively. Applicable for unix type operation systems.
     *
     * @param path to directory
     * @throws IOException during watchers registration
     */
    private void registerWatchersRecursively(final Path path) throws IOException {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(final Path dir,
                                                     final BasicFileAttributes attrs) throws IOException {
                LOG.trace("Registering in watcher server: {}", () -> dir);
                final WatchKey watchKey = dir
                        .register(watchService,
                                StandardWatchEventKinds.ENTRY_CREATE,
                                StandardWatchEventKinds.ENTRY_DELETE,
                                StandardWatchEventKinds.ENTRY_MODIFY);
//                 , SensitivityWatchEventModifier.HIGH);
                if (watchKeyConsumer != null) {
                    watchKeyConsumer.accept(watchKey);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
项目:launchpad-missioncontrol    文件:FileUploadHelper.java   
/**
 * Deletes a directory recursively
 *
 * @param directory
 * @return true if deletion succeeds, false otherwise
 */
public static boolean deleteDirectory(Path directory) {
    if (directory != null) {
        try {
            Files.walkFileTree(directory, 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;
                }
            });
        } catch (IOException ignored) {
            return false;
        }
    }
    return true;
}
项目:openjdk9    文件:ClasspathDependencies.java   
static void delete(Path root) throws IOException {
    if (!Files.exists(root))
        return;
    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
             @Override
             public FileVisitResult visitFile(Path f, BasicFileAttributes a)
                     throws IOException {
                 Files.delete(f);
                 return FileVisitResult.CONTINUE;
             }

             @Override
             public FileVisitResult postVisitDirectory(Path dir, IOException e)
                     throws IOException {
                 if (e != null)
                     throw e;
                 if (!dir.equals(root))
                     Files.delete(dir);
                 return FileVisitResult.CONTINUE;
             }
        });
}
项目:openjdk9    文件:JmodTask.java   
void processSection(ZipOutputStream zos, Section section, Path top)
    throws IOException
{
    final String prefix = section.jmodDir();

    Files.walkFileTree(top, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
            throws IOException
        {
            Path relPath = top.relativize(file);
            if (!relPath.toString().equals(MODULE_INFO)
                    && !matches(relPath, excludes)) {
                try (InputStream in = Files.newInputStream(file)) {
                    writeZipEntry(zos, in, prefix, relPath.toString());
                }
            }
            return FileVisitResult.CONTINUE;
        }
    });
}