Refine search
/** * 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; } }); }
/** * 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; } } }); }
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 {
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; }
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; }
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));
/** * 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; } }); }
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); } }); }
/** * 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()); } }
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(); }
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; } }); } }
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) {
public void clean() { try { if (tempDir.exists()) { Files.walkFileTree(tempDir.toPath(), DeleteRecursivelyFileVisitor.INSTANCE); } } catch (IOException e) { LOG.error("Failed to delete temp folder", e); } }
throws IOException{ final Path destDir = Paths.get(destDirname); Files.walkFileTree(root, new SimpleFileVisitor<Path>(){ @Override public FileVisitResult visitFile(Path file,
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; } }); } }
Files.walkFileTree(Paths.get(inputDataPath), finder); long allFileSize = 0; for (Long everyFileSize : fileSizeList) {
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; }
final Path src = Paths.get(filename); Files.walkFileTree(src, new SimpleFileVisitor<Path>(){ @Override public FileVisitResult visitFile(Path file,
@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; } }); }
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 {