/** * {@inheritDoc} */ @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (deleteSource) { return super.postVisitDirectory(dir, exc); } return FileVisitResult.CONTINUE; }
/** * {@inheritDoc} */ @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.copy(file, toPath.resolve(fromPath.relativize(file)), StandardCopyOption.REPLACE_EXISTING); if (deleteSource) { return super.visitFile(file, attrs); } else { return FileVisitResult.CONTINUE; } }
/** * Deletes all files associated with given {@link StorageData}, thus completely removes storage * from disk. * * @param storageData * Storage to delete data for. * @throws IOException * If {@link IOException} happens. */ protected void deleteCompleteStorageDataFromDisk(IStorageData storageData) throws IOException { Path storageDir = getStoragePath(storageData); if (log.isDebugEnabled()) { log.info("Deleting the complete storage data from disk. Path: " + storageDir); } if (Files.exists(storageDir)) { Files.walkFileTree(storageDir, new DeleteFileVisitor()); } }
/** * Clean test folder after each test. */ @AfterMethod public void cleanUp() throws IOException { if (Files.exists(Paths.get(TEST_FOLDER))) { Files.walkFileTree(Paths.get(TEST_FOLDER), new DeleteFileVisitor()); Files.deleteIfExists(Paths.get(TEST_FOLDER)); } }
/** * Clean test folder after each test. */ @AfterMethod public void cleanUp() throws IOException { if (Files.exists(Paths.get(TEST_FOLDER))) { Files.walkFileTree(Paths.get(TEST_FOLDER), new DeleteFileVisitor()); Files.deleteIfExists(Paths.get(TEST_FOLDER)); } } }
/** * {@inheritDoc} * <p> * Only deletes the files that have extension matching the supplied {@link #storageFileTypes}. */ @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (ArrayUtils.isNotEmpty(storageFileTypes)) { for (StorageFileType storageFileType : storageFileTypes) { if (file.endsWith(storageFileType.getExtension())) { return super.visitFile(file, attrs); } } } return FileVisitResult.CONTINUE; }
/** * {@inheritDoc} */ @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (deleteDirs) { try { return super.postVisitDirectory(dir, exc); } catch (IOException e) { // if directory delete fails, we still want to continue return FileVisitResult.CONTINUE; } } return FileVisitResult.CONTINUE; }
@AfterTest public void cleanUp() throws IOException { if (Files.exists(testPath)) { Files.walkFileTree(testPath, new DeleteFileVisitor()); } Files.deleteIfExists(testPath); }
/** * Test that deleting of directory and it content works, but that the parent dir won't be * touched. */ @Test public void deleteFileVisitor() throws IOException { // we create additional dir and file in that dir Path dir = testPath.resolve("dir"); Files.createDirectories(dir); Path file = dir.resolve(".testFile"); Files.createFile(file); Files.walkFileTree(dir, new DeleteFileVisitor()); assertThat(Files.exists(file), is(false)); assertThat(Files.exists(dir), is(false)); assertThat(Files.exists(testPath), is(true)); }
Files.walkFileTree(parentDir, new DeleteFileVisitor());