List of usage examples for java.nio.file Files walkFileTree
public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException
From source file:org.phenotips.variantstore.input.vcf.VCFManager.java
@Override public List<String> getAllIndividuals() { final List<String> list = new ArrayList<>(); try {/*from ww w . jav a 2 s . c om*/ Files.walkFileTree(this.path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (attrs.isDirectory()) { return FileVisitResult.CONTINUE; } String id = file.getFileName().toString(); id = StringUtils.removeEnd(id, suffix); list.add(id); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { logger.error("Error getting all individuals"); } return list; }
From source file:ufo.test.spark.service.WordCountServiceTest.java
private void deleteRecursively(Path path) throws IOException { if (Files.exists(path)) { //delete recursively Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override/* w w w . ja v a 2s. c om*/ 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; } }); } assertFalse(Files.exists(path)); }
From source file:org.esa.nest.util.FileIOUtils.java
public static void deleteFolder(final Path source) throws IOException { Files.walkFileTree(source, new SimpleFileVisitor<Path>() { @Override//from w w w.j a v a2s. c o m public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc == null) { Files.delete(dir); return CONTINUE; } else { throw exc; } } }); }
From source file:ch.bender.evacuate.Runner.java
/** * run/*from ww w . j a v a 2 s .c o m*/ * <p> * @throws Exception */ public void run() throws Exception { checkDirectories(); initExcludeMatchers(); myEvacuateCandidates = new TreeMap<>(); myFailedChainPreparations = Collections.synchronizedMap(new HashMap<>()); myFutures = new HashSet<>(); myExclusionDirCount = 0; myEvacuationDirCount = 0; myExclusionFileCount = 0; myEvacuationFileCount = 0; Files.walkFileTree(myBackupDir, new SimpleFileVisitor<Path>() { /** * @see java.nio.file.SimpleFileVisitor#visitFileFailed(java.lang.Object, java.io.IOException) */ @Override public FileVisitResult visitFileFailed(Path aFile, IOException aExc) throws IOException { if ("System Volume Information".equals((aFile.getFileName().toString()))) { return FileVisitResult.SKIP_SUBTREE; } throw aExc; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { return Runner.this.visitFile(file, attrs); } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if ("System Volume Information".equals((dir.getFileName()))) { return FileVisitResult.SKIP_SUBTREE; } return Runner.this.preVisitDirectory(dir, attrs); } }); if (myEvacuateCandidates.size() == 0) { myLog.info("No candidates for evacuation found"); } else { StringBuilder sb = new StringBuilder("\nFound candidates for evacuation:"); myEvacuateCandidates.keySet().forEach(p -> sb.append("\n " + p.toString())); myLog.info(sb.toString()); } if (myDryRun) { myLog.debug("DryRun flag is set. Doing nothing"); return; } if (myFutures.size() > 0) { myLog.debug("Waiting for all async tasks to complete"); CompletableFuture.allOf(myFutures.toArray(new CompletableFuture[myFutures.size()])).get(); } if (myFailedChainPreparations.size() > 0) { for (Path path : myFailedChainPreparations.keySet()) { myLog.error("exception occured", myFailedChainPreparations.get(path)); } throw new Exception("chain preparation failed. See above error messages"); } for (Path src : myEvacuateCandidates.keySet()) { Path dst = myEvacuateCandidates.get(src); Path dstParent = dst.getParent(); if (Files.notExists(dstParent)) { Files.createDirectories(dstParent); // FUTURE: overtake file attributes from src } if (myMove) { try { myLog.debug( "Moving file system object \"" + src.toString() + "\" to \"" + dst.toString() + "\""); Files.move(src, dst, StandardCopyOption.ATOMIC_MOVE); } catch (AtomicMoveNotSupportedException e) { myLog.warn("Atomic move not supported. Try copy and then delete"); if (Files.isDirectory(src)) { myLog.debug("Copying folder \"" + src.toString() + "\" to \"" + dst.toString() + "\""); FileUtils.copyDirectory(src.toFile(), dst.toFile()); myLog.debug("Delete folder \"" + src.toString() + "\""); FileUtils.deleteDirectory(src.toFile()); } else { myLog.debug("Copy file \"" + src.toString() + "\" to \"" + dst.toString() + "\""); FileUtils.copyFile(src.toFile(), dst.toFile()); myLog.debug("Delete file \"" + src.toString() + "\""); Files.delete(src); } } } else { if (Files.isDirectory(src)) { myLog.debug("Copying folder \"" + src.toString() + "\" to \"" + dst.toString() + "\""); FileUtils.copyDirectory(src.toFile(), dst.toFile()); } else { myLog.debug("Copy file \"" + src.toString() + "\" to \"" + dst.toString() + "\""); FileUtils.copyFile(src.toFile(), dst.toFile()); } } } myLog.info("\nSuccessfully terminated." + "\n Evacuated Skipped" + "\n Files : " + StringUtils.leftPad("" + myEvacuationDirCount, 9) + StringUtils.leftPad("" + myExclusionDirCount, 9) + "\n Folders: " + StringUtils.leftPad("" + myEvacuationFileCount, 9) + StringUtils.leftPad("" + myExclusionFileCount, 9)); }
From source file:neembuu.uploader.zip.generator.NUZipFileGenerator.java
private void walkOverAllFiles() throws IOException { for (final Path uploadersDirectory : uploadersDirectories) { Files.walkFileTree(uploadersDirectory, new FileVisitor<Path>() { @Override/*w ww .j a va2s. co m*/ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { exc.printStackTrace(); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.getFileName().toString().endsWith(".class")) { visitClassFile(file, attrs, uploadersDirectory); } return FileVisitResult.CONTINUE; } }); } }
From source file:org.nuxeo.connect.update.standalone.PackageTestCase.java
/** Zips a directory into the given ZIP file. */ protected void createZip(File zip, Path basePath) throws IOException { try (ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zip)))) { Files.walkFileTree(basePath, new SimpleFileVisitor<Path>() { @Override/* w w w .j ava 2s. com*/ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (attrs.isDirectory()) { return FileVisitResult.CONTINUE; } String rel = basePath.relativize(path).toString(); if (rel.startsWith(".")) { return FileVisitResult.CONTINUE; } zout.putNextEntry(new ZipEntry(rel)); try (InputStream in = Files.newInputStream(path)) { org.apache.commons.io.IOUtils.copy(in, zout); } zout.closeEntry(); return FileVisitResult.CONTINUE; } }); zout.flush(); } }
From source file:org.neo4j.graphdb.LuceneLabelScanStoreIT.java
private void corruptIndex(NeoStoreDataSource dataSource) throws IOException { File labelScanStoreDirectory = getLabelScanStoreDirectory(dataSource); Files.walkFileTree(labelScanStoreDirectory.toPath(), new SimpleFileVisitor<Path>() { @Override//from w ww . ja v a2 s .c o m public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.write(file, ArrayUtils.add(Files.readAllBytes(file), (byte) 7)); return FileVisitResult.CONTINUE; } }); }
From source file:org.schedulesdirect.grabber.Auditor.java
private void auditScheds() throws IOException, JSONException, ParseException { final Map<String, JSONObject> stations = getStationMap(); final SimpleDateFormat FMT = Config.get().getDateTimeFormat(); final Path scheds = vfs.getPath("schedules"); if (Files.isDirectory(scheds)) { Files.walkFileTree(scheds, new FileVisitor<Path>() { @Override//from ww w . j a v a 2s .c om public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return dir.equals(scheds) ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { boolean failed = false; String id = getStationIdFromFileName(file.getFileName().toString()); JSONObject station = stations.get(id); StringBuilder msg = new StringBuilder(String.format("Inspecting %s (%s)... ", station != null ? station.getString("callsign") : String.format("[UNKNOWN: %s]", id), id)); String input; try (InputStream ins = Files.newInputStream(file)) { input = IOUtils.toString(ins, ZipEpgClient.ZIP_CHARSET.toString()); } ObjectMapper mapper = Config.get().getObjectMapper(); JSONArray jarr = mapper.readValue( mapper.readValue(input, JSONObject.class).getJSONArray("programs").toString(), JSONArray.class); for (int i = 1; i < jarr.length(); ++i) { long start, prevStart; JSONObject prev; try { start = FMT.parse(jarr.getJSONObject(i).getString("airDateTime")).getTime(); prev = jarr.getJSONObject(i - 1); prevStart = FMT.parse(prev.getString("airDateTime")).getTime() + 1000L * prev.getLong("duration"); } catch (ParseException e) { throw new RuntimeException(e); } if (prevStart != start) { msg.append(String.format("FAILED! [%s]", prev.getString("airDateTime"))); LOG.error(msg); failed = true; Auditor.this.failed = true; break; } } if (!failed) { msg.append("PASSED!"); LOG.info(msg); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { LOG.error(String.format("Unable to process schedule file '%s'", file), exc); Auditor.this.failed = true; return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); } }
From source file:org.swiftexplorer.util.FileUtils.java
public static Queue<Path> getAllFilesPath(Path srcDir, boolean inludeDir) throws IOException { Queue<Path> queue = new ArrayDeque<Path>(); if (srcDir == null || !Files.isDirectory(srcDir)) return queue; TreePathFinder tpf = new TreePathFinder(queue, inludeDir); Files.walkFileTree(srcDir, tpf); return queue; }
From source file:act.installer.pubchem.PubchemTTLMergerTest.java
@After public void tearDown() throws Exception { // Clean up temp dir once the test is complete. TODO: use mocks instead maybe? But testing RocksDB helps too... /* With help from://ww w . ja v a 2s. c o m * http://stackoverflow.com/questions/779519/delete-directories-recursively-in-java/27917071#27917071 */ Files.walkFileTree(tempDirPath, 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 { // walkFileTree may ignore . and .., but I have never found it a /bad/ idea to check for these special names. if (!THIS_DIR.equals(file.toFile().getName()) && !PARENT_DIR.equals(file.toFile().getName())) { Files.delete(file); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { throw exc; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); // One last check to make sure the top level directory is removed. if (tempDirPath.toFile().exists()) { Files.delete(tempDirPath); } }