List of usage examples for java.nio.file FileVisitor FileVisitor
FileVisitor
From source file:com.excelsiorjet.api.util.Utils.java
public static void cleanDirectory(File f) throws IOException { Files.walkFileTree(f.toPath(), new FileVisitor<Path>() { @Override// www .java2 s . co m public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return FileVisitResult.CONTINUE; } private void deleteFile(File f) throws IOException { if (!f.delete()) { if (f.exists()) { throw new IOException(Txt.s("JetApi.UnableToDelete.Error", f.getAbsolutePath())); } } } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { deleteFile(file.toFile()); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { if (file.toFile().exists()) { throw new IOException(Txt.s("JetApi.UnableToDelete.Error", f.getAbsolutePath())); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { deleteFile(dir.toFile()); return FileVisitResult.CONTINUE; } }); }
From source file:file.FileOperatorTest.java
public static List<Path> testWalkFile() throws Exception { List<Path> paths = Lists.newArrayList(); long a = System.currentTimeMillis(); Files.walkFileTree(Paths.get(ROOT_DIR), new FileVisitor<Path>() { @Override//from w w w.j a v a2 s . c o m public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { paths.add(file); 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; } }); long b = System.currentTimeMillis(); System.out.println(":" + (b - a)); return paths; }
From source file:org.eclipse.vorto.codegen.api.CopyResourceTask.java
public void generate(Context metaData, IMappingContext mappingContext, final IGeneratedWriter outputter) { try {/* w w w. j ava 2s . c o m*/ Path start = Paths.get(basePath.toURI()); Files.walkFileTree(start, 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 { outputter.write(new Generated(file.getFileName().toFile().getName(), getOutputPath(file).isEmpty() ? null : getOutputPath(file), FileUtils.readFileToByteArray((file.toFile())))); 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; } }); } catch (IOException ioEx) { throw new RuntimeException(ioEx); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.apache.openaz.xacml.pdp.test.conformance.ConformanceTestSet.java
public static ConformanceTestSet loadDirectory(File fileDir) throws IOException { final Map<String, ConformanceTest> mapConformanceTests = new HashMap<String, ConformanceTest>(); Files.walkFileTree(fileDir.toPath(), new FileVisitor<Path>() { @Override/* ww w . j ava 2s . c om*/ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { logger.info("Scanning directory " + dir.getFileName()); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { File fileVisited = file.toFile(); String fileName = fileVisited.getName(); if (fileName.endsWith(".xml") || fileName.endsWith(".properties")) { String testName = getTestName(fileVisited); if (testName != null) { ConformanceTest conformanceTest = mapConformanceTests.get(testName); if (conformanceTest == null) { logger.info("Added test " + testName); conformanceTest = new ConformanceTest(testName); mapConformanceTests.put(testName, conformanceTest); } if (fileName.endsWith("Policy.xml")) { conformanceTest.getRepository().addRootPolicy(fileVisited); } else if (fileName.endsWith("Repository.properties")) { conformanceTest.getRepository().load(fileVisited); } else if (fileName.endsWith("Request.xml")) { conformanceTest.setRequest(fileVisited); } else if (fileName.endsWith("Response.xml")) { conformanceTest.setResponse(fileVisited); } } } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { logger.warn("Skipped " + file.getFileName()); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); /* * Sort the keyset and pull out the tests that have the required components */ List<String> listTestNames = new ArrayList<String>(); listTestNames.addAll(mapConformanceTests.keySet()); Collections.sort(listTestNames); ConformanceTestSet conformanceTestSet = new ConformanceTestSet(); Iterator<String> iterTestNames = listTestNames.iterator(); while (iterTestNames.hasNext()) { ConformanceTest conformanceTest = mapConformanceTests.get(iterTestNames.next()); if (conformanceTest.isComplete()) { conformanceTestSet.addConformanceTest(conformanceTest); logger.debug("Added conformance test " + conformanceTest.getTestName()); } else { logger.warn("Incomplete conformance test " + conformanceTest.getTestName()); } } return conformanceTestSet; }
From source file:de.alexkamp.sandbox.ChrootSandboxFactory.java
@Override public void deleteSandbox(final SandboxData sandbox) { File copyDir = sandbox.getBaseDir(); try {//from www.j a v a 2 s . c o m final AtomicInteger depth = new AtomicInteger(0); Files.walkFileTree(copyDir.toPath(), new FileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes) throws IOException { int d = depth.getAndIncrement(); // see whether the mounts are gone if (1 == d) { for (Mount m : sandbox) { if (path.endsWith(m.getMountPoint().substring(1))) { if (0 != path.toFile().listFiles().length) { throw new IllegalArgumentException( path.getFileName() + " has not been unmounted."); } } } } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException { Files.delete(path); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path path, IOException e) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path path, IOException e) throws IOException { Files.delete(path); depth.decrementAndGet(); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { throw new SandboxException(e); } }
From source file:org.jetbrains.webdemo.backend.executor.ExecutorUtils.java
public static ExecutionResult executeCompiledFiles(Map<String, byte[]> files, String mainClass, List<Path> kotlinRuntimeJars, String arguments, Path executorsPolicy, boolean isJunit) throws Exception { Path codeDirectory = null;//from w w w. ja v a2s.c o m try { codeDirectory = storeFilesInTemporaryDirectory(files); JavaExecutorBuilder executorBuilder = new JavaExecutorBuilder().enableAssertions().setMemoryLimit(32) .enableSecurityManager().setPolicyFile(executorsPolicy).addToClasspath(kotlinRuntimeJars) .addToClasspath(jarFiles).addToClasspath(codeDirectory); if (isJunit) { executorBuilder.addToClasspath(junit); executorBuilder.addToClasspath(hamcrest); executorBuilder.setMainClass("org.jetbrains.webdemo.executors.JunitExecutor"); executorBuilder.addArgument(codeDirectory.toString()); } else { executorBuilder.setMainClass("org.jetbrains.webdemo.executors.JavaExecutor"); executorBuilder.addArgument(mainClass); } for (String argument : arguments.split(" ")) { if (argument.trim().isEmpty()) continue; executorBuilder.addArgument(argument); } ProgramOutput output = executorBuilder.build().execute(); return parseOutput(output.getStandardOutput(), isJunit); } finally { try { if (codeDirectory != null) { Files.walkFileTree(codeDirectory, 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.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { return FileVisitResult.TERMINATE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } } catch (IOException e) { ErrorWriter.getInstance().writeExceptionToExceptionAnalyzer(e, "Can't delete code directory"); } } }
From source file:com.excelsiorjet.api.util.Utils.java
public static void copyDirectory(Path source, Path target) throws IOException { Files.walkFileTree(source, new FileVisitor<Path>() { @Override/*from w w w . j av a2 s . c om*/ public FileVisitResult preVisitDirectory(Path subfolder, BasicFileAttributes attrs) throws IOException { Files.createDirectories(target.resolve(source.relativize(subfolder))); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path sourceFile, BasicFileAttributes attrs) throws IOException { Path targetFile = target.resolve(source.relativize(sourceFile)); copyFile(sourceFile, targetFile); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path sourceFile, IOException e) throws IOException { throw new IOException(Txt.s("Utils.CannotCopyFile.Error", sourceFile.toString(), e.getMessage()), e); } @Override public FileVisitResult postVisitDirectory(Path source, IOException ioe) throws IOException { return FileVisitResult.CONTINUE; } }); }
From source file:fr.ortolang.diffusion.store.binary.BinaryStoreServiceTest.java
@After public void tearDown() { try {//from www. j a v a 2 s . c o m Files.walkFileTree(service.getBase(), 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.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { LOGGER.log(Level.SEVERE, "unable to purge temporary created filesystem", exc); return FileVisitResult.TERMINATE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); } }
From source file:br.com.uol.runas.classloader.JarClassLoader.java
private void addUrlsFromDir(URL url) throws IOException, URISyntaxException { addURL(url);/* ww w. j a v a 2s. c o m*/ Files.walkFileTree(Paths.get(url.toURI()), new FileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { addURL(dir.toUri().toURL()); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { addURL(file.toUri().toURL()); 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; } }); }
From source file:org.hawkular.apm.api.services.ConfigurationLoader.java
/** * This method loads the configuration from the supplied URI. * * @param uri The URI/*from w ww . j ava2s .c om*/ * @param type The type, or null if default (jvm) * @return The configuration */ protected static CollectorConfiguration loadConfig(String uri, String type) { final CollectorConfiguration config = new CollectorConfiguration(); if (type == null) { type = DEFAULT_TYPE; } uri += java.io.File.separator + type; File f = new File(uri); if (!f.isAbsolute()) { if (f.exists()) { uri = f.getAbsolutePath(); } else if (System.getProperties().containsKey("jboss.server.config.dir")) { uri = System.getProperty("jboss.server.config.dir") + java.io.File.separatorChar + uri; } else { try { URL url = Thread.currentThread().getContextClassLoader().getResource(uri); if (url != null) { uri = url.getPath(); } else { log.severe("Failed to get absolute path for uri '" + uri + "'"); } } catch (Exception e) { log.log(Level.SEVERE, "Failed to get absolute path for uri '" + uri + "'", e); uri = null; } } } if (uri != null) { String[] uriParts = uri.split(Matcher.quoteReplacement(File.separator)); int startIndex = 0; // Remove any file prefix if (uriParts[0].equals("file:")) { startIndex++; } try { Path path = getPath(startIndex, uriParts); Files.walkFileTree(path, new FileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(Path path, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (path.toString().endsWith(".json")) { String json = new String(Files.readAllBytes(path)); CollectorConfiguration childConfig = mapper.readValue(json, CollectorConfiguration.class); if (childConfig != null) { config.merge(childConfig, false); } } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path path, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); } catch (Throwable e) { log.log(Level.SEVERE, "Failed to load configuration", e); } } return config; }