Java Code Examples for java.nio.file.Files#walkFileTree()

The following examples show how to use java.nio.file.Files#walkFileTree() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IOUtils.java    From oryx with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
  });
}
 
Example 2
Source File: TestSolrCloudClusterSupport.java    From storm-solr with Apache License 2.0 6 votes vote down vote up
@AfterClass
public static void stopCluster() throws Exception {
  if (cloudSolrClient != null) {
    cloudSolrClient.shutdown();
  }
  if (cluster != null) {
    cluster.shutdown();
  }

  // Delete tempDir content
  Files.walkFileTree(tempDir, 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;
    }

  });
}
 
Example 3
Source File: IO.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
public static AutoCloseable autoDir(final Path workDir) throws IOException {
    final boolean exists = Files.exists(workDir);
    if (exists) {
        return () -> {
        };
    }
    log.debug("Creating '{}'", workDir);
    Files.createDirectories(workDir);
    return () -> Files.walkFileTree(workDir, new SimpleFileVisitor<Path>() {

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

        @Override
        public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
            Files.delete(dir);
            if (dir.equals(workDir)) {
                log.debug("Deleted '{}'", dir);
            }
            return super.postVisitDirectory(dir, exc);
        }
    });
}
 
Example 4
Source File: DirectoryScannerStageIntegrationTest.java    From kieker with Apache License 2.0 6 votes vote down vote up
private void deleteRecursively(final Path path) throws IOException {
	Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
		@Override
		public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
			Files.delete(file);
			return FileVisitResult.CONTINUE;
		}

		@Override
		public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
			Files.delete(dir);
			return FileVisitResult.CONTINUE;
		}
	});

}
 
Example 5
Source File: LogFileResourceDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static void validateFile(final OperationContext context, final String logDir, final String fileName) throws OperationFailedException {
    // Ensure the resource exists
    context.readResource(PathAddress.EMPTY_ADDRESS);
    final Path dir = Paths.get(logDir);
    final AtomicBoolean found = new AtomicBoolean();
    try {
        // Walk the log directory and only allow files within the log directory to be read
        Files.walkFileTree(dir, Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                final Path relativeFile = dir.relativize(file);
                if (fileName.equals(relativeFile.toString())) {
                    found.set(true);
                    return FileVisitResult.TERMINATE;
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        throw LoggingLogger.ROOT_LOGGER.failedToReadLogFile(e, fileName);
    }
    if (!found.get()) {
        throw LoggingLogger.ROOT_LOGGER.readNotAllowed(fileName);
    }
}
 
Example 6
Source File: FileUtils.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Deletes a file or directory and all contents recursively.
 *
 * @param path the file to delete.
 */
public static void deleteRecursively(Path path) throws IOException {
    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 FileVisitResult.CONTINUE;
        }
    });
}
 
Example 7
Source File: SizeAppenderRestartTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Iterable<Path> listLogFiles() throws IOException {
    final Collection<Path> names = new ArrayList<>();
    Files.walkFileTree(logFile.getParent(), new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            names.add(file);
            return super.visitFile(file, attrs);
        }
    });
    return names;
}
 
Example 8
Source File: ClassFileSource.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
FromFilePath(Path path, ImportOptions importOptions) {
    this.importOptions = importOptions;
    if (path.toFile().exists()) {
        try {
            Files.walkFileTree(path, this);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 9
Source File: GradleUpdater.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
private void processAllGradleProps(ReleaserProperties properties, File projectRoot,
		Projects projects, ProjectVersion versionFromBom, boolean assertVersions) {
	try {
		Files.walkFileTree(projectRoot.toPath(), new GradlePropertiesWalker(
				properties, projects, versionFromBom, assertVersions));
	}
	catch (IOException e) {
		throw new IllegalStateException(e);
	}
}
 
Example 10
Source File: LogFileService.java    From c5-replicator with Apache License 2.0 5 votes vote down vote up
/**
 * Delete all the logs stored in the wal root directory.
 *
 * @throws IOException
 */
public void clearAllLogs() throws IOException {
  Files.walkFileTree(logRootDir, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
      if (!attrs.isDirectory()) {
        Files.delete(file);
      }
      return FileVisitResult.CONTINUE;
    }
  });
}
 
Example 11
Source File: RecursiveDirectoryRemover.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @param path           The starting point.
 * @param includeRootDir Pass in {@code true} to remove the specified path as well as its
 *                       contents.
 */
public static final void remove(Path path, boolean includeRootDir) {
    try {
        Files.walkFileTree(path, new RecursiveDirectoryRemover(includeRootDir ? null : path));
    } catch (IOException exception) {
        Log.error(exception);
    }
}
 
Example 12
Source File: ToolkitAPITest.java    From streamsx.topology with Apache License 2.0 5 votes vote down vote up
@Test
public void testZip() throws Exception {
  Path tkpath = bingo0Path.toPath();
  File baseDir = new File(System.getProperty("java.io.tmpdir"));
  Path targetParent = Files.createTempDirectory(baseDir.toPath(), "tz");
  try {
    Path target = new File(targetParent.toFile(),"tk.zip").toPath();
    try (InputStream is = DirectoryZipInputStream.fromPath(tkpath)) {
      Files.copy(is, target);
    }

    // Unzip the zip file with system tools and compare with the source
    // directory.  
    StringBuilder command = new StringBuilder();
    command.append("cd '").append(targetParent.toFile().getAbsolutePath()).append("' && ");
    command.append("unzip -qq tk.zip && ");
    command.append("diff -r bingo_tk0 '").append(bingo0Path.getAbsolutePath()).append("'");
    System.out.println(command);

    ProcessBuilder builder = new ProcessBuilder("bash","-c",command.toString());
    Process process = builder.start();
    int exitCode = process.waitFor();
  
    copy (process.getInputStream(), System.out);
    copy (process.getErrorStream(), System.err);

    assertEquals(0, exitCode);

  }
  finally {
    // Delete the temp files and contents this when test is done.  
    Files.walkFileTree(targetParent, new DirectoryDeletor());
  }
}
 
Example 13
Source File: MCRFileUploadBucket.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static synchronized void releaseBucket(String bucketID) {
    if (BUCKET_MAP.containsKey(bucketID)) {
        final MCRFileUploadBucket bucket = BUCKET_MAP.get(bucketID);
        if (Files.exists(bucket.root)) {
            try {
                Files.walkFileTree(bucket.root, MCRRecursiveDeleter.instance());
            } catch (IOException e) {
                throw new MCRUploadException("MCR.Upload.TempDirectory.Delete.Failed", e);
            }
        }
        BUCKET_MAP.remove(bucketID);
    }
}
 
Example 14
Source File: AbstractContentUpgradeOperation.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Finds all files in the given site that match any of the given patterns
 * @param site the site id
 * @return the list of matching files
 * @throws IOException if there is any error finding the files
 */
protected List<Path> findIncludedPaths(final String site) throws IOException {
    if(CollectionUtils.isNotEmpty(includedPaths)) {
        Path repo = getRepositoryPath(site).getParent();
        ListFileVisitor fileVisitor = new ListFileVisitor(repo, includedPaths);
        Files.walkFileTree(repo, fileVisitor);
        return fileVisitor.getMatchedPaths();
    }
    return null;
}
 
Example 15
Source File: NbMavenProjectImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public File[] getOtherRoots(boolean test) {
    URI uri = FileUtilities.getDirURI(getProjectDirectory(), test ? "src/test" : "src/main"); //NOI18N
    Set<File> toRet = new HashSet<File>();
    File fil = Utilities.toFile(uri);
    if (fil.exists()) {
        try {
            Path sourceRoot = fil.toPath();
            OtherRootsVisitor visitor = new OtherRootsVisitor(getLookup(), sourceRoot);
            Files.walkFileTree(sourceRoot, visitor);
            toRet.addAll(visitor.getOtherRoots());
        } catch (IOException ex) {
            // log as info to keep trace about possible problems, 
            // but lets not be too agressive with level and notification                
            // see also issue #251071
            LOG.log(Level.INFO, null, ex);
        }
    }
    URI[] res = getResources(test);
    for (URI rs : res) {
        File fl = Utilities.toFile(rs);
        //in node view we need only the existing ones, if anything else needs all,
        // a new method is probably necessary..
        if (fl.exists()) {
            toRet.add(fl);
        }
    }
    return toRet.toArray(new File[0]);
}
 
Example 16
Source File: FileWalker.java    From PeerWasp with MIT License 5 votes vote down vote up
/**
 * This method discovers the structure of the subtree defined by
 * {@link rootFolder}. The structure is defined as a recursive hash
 * over the {@link Path}s of all contained files and folders.
 * 
 * @return a hash representing the subtree's recursive structure.
 */
public String computeStructureHashOfFolder(){
	try {
		Files.walkFileTree(rootFolder, new FileIndexer(false));

	} catch (IOException e) {
		e.printStackTrace();
	}
	return fileTree.getStructureHash();
}
 
Example 17
Source File: FileSynchronizeService.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
public void synchronize() {
    String prefix = dir.toString().replaceAll("\\\\", "/");
    try {
        Files.walkFileTree(dir, new PathSimpleFileVisitor(prefix, fileService, directoryService));
    } catch (IOException e) {
        throw new ResourceException("failed to synchronize files", e);
    }
}
 
Example 18
Source File: AnalysisRunner.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Create a jar file which contains all resource files. This is necessary to
 * let {@link Plugin#loadCustomPlugin(File, Project)} load custom plugin to
 * test.
 *
 * @return a {@link File} instance which represent generated jar file
 * @throws IOException
 * @throws URISyntaxException
 */
private static File createTempJar() throws IOException, URISyntaxException {
    ClassLoader cl = AnalysisRunner.class.getClassLoader();

    URL resource = cl.getResource("findbugs.xml");
    URI uri = resource.toURI();

    if ("jar".equals(uri.getScheme())) {
        JarURLConnection connection = (JarURLConnection) resource.openConnection();
        URL url = connection.getJarFileURL();
        return new File(url.toURI());
    }

    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();

        byte[] data = new byte[4 * 1024];
        Files.walkFileTree(resourceRoot, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String name = resourceRoot.relativize(file).toString();
                jar.putNextEntry(new ZipEntry(name));
                try (InputStream input = Files.newInputStream(file, StandardOpenOption.READ)) {
                    int len;
                    while ((len = input.read(data)) > 0) {
                        jar.write(data, 0, len);
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
    return tempJar.toFile();
}
 
Example 19
Source File: FileListener.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
/**
 *  Start a file listener (WatchService) and run a service of a given name and writes the result to the database
 *  Can be used to implements EECAs to auto-update information based on file changes
 **/
public static void startFileListener(String name, String location){

    try{
        if(UtilValidate.isNotEmpty(name) && UtilValidate.isNotEmpty(location)){

            if(getThreadByName(name)!=null){
                Debug.logInfo("Filelistener "+name+" already started. Skipping...", module);
            }else{
                URL resLocation = UtilURL.fromResource(location);
                Path folderLocation = Paths.get(resLocation.toURI());
                if(folderLocation == null) {
                    throw new UnsupportedOperationException("Directory not found");
                }


                final WatchService folderWatcher = folderLocation.getFileSystem().newWatchService();
                // register all subfolders
                Files.walkFileTree(folderLocation, new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                        dir.register(folderWatcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY);
                        return FileVisitResult.CONTINUE;
                    }
                });

                // start the file watcher thread below
                ScipioWatchQueueReader fileListener = new ScipioWatchQueueReader(folderWatcher,name,location);
                ScheduledExecutorService executor = ExecutionPool.getScheduledExecutor(FILE_LISTENER_THREAD_GROUP, "filelistener-startup", Runtime.getRuntime().availableProcessors(), 0, true);
                try {
                    executor.submit(fileListener, name);
                } finally {
                    executor.shutdown();
                }
                Debug.logInfo("Starting FileListener thread for "+name, module);
            }
        }
    }catch(Exception e){
        Debug.logError("Could not start FileListener "+name+" for "+location+"\n"+e, module);

    }
}
 
Example 20
Source File: Runner.java    From evosql with Apache License 2.0 4 votes vote down vote up
public static void evaluateProject(String projectName, String algorithm) throws IOException, ClassNotFoundException {
		// Either find the project's path, or you are already in the project's path
		Path basePath = Paths.get(System.getProperty("user.dir"));
		String projectPath;
		if (basePath.getName(basePath.getNameCount() - 2).toString().contains("scenarios")) {
			projectPath = basePath.toString() + "/";
		} else {
			String path = Paths.get(System.getProperty("user.dir")).toString() + "/scenarios/";
			projectPath = path + projectName + "/";
		}
		
		Properties props = new Properties();
		props.load(new FileInputStream(projectPath + "config.properties"));
		
		List<ScenarioQuery> queries = getQueries(projectPath);
		String schema = getSchema(projectPath);
		
		String resultsPath = projectPath + "results-" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new java.util.Date()) + "/";

		String testDataFolder = resultsPath + "testdata/";
		Files.createDirectories(Paths.get(testDataFolder));
		
		PrintStream output = new PrintStream(resultsPath + "results.csv");
		PrintStream coverageOutput = new PrintStream(resultsPath + "coverageResults.csv");
		
		QueryPathReader pathReader = new QueryPathReader(projectPath);

		Evaluation eval = null;

		Map<String, TableSchema> schemas = getMockedSchemas(projectPath);

		if(schemas == null) {
			eval = new Evaluation(
					props.getProperty("url"),
					props.getProperty("database"),
					props.getProperty("schema"),
					props.getProperty("user"),
					props.getProperty("pwd"),
					schema,
					queries,
					output,
					coverageOutput,
					testDataFolder,
					pathReader,
					algorithm);
		} else {
			eval = new Evaluation(schemas, queries, output, coverageOutput, testDataFolder, pathReader, algorithm);
		}

//		eval.perform(1016, 19);
		eval.perform();

		coverageOutput.close();
		output.close();
		
		// Remove mem directory if exists
		Path directory = Paths.get(projectPath + "mem");
		if (Files.exists(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;
			   }
			});
		}
	}