Java Code Examples for org.springframework.util.FileSystemUtils#deleteRecursively()

The following examples show how to use org.springframework.util.FileSystemUtils#deleteRecursively() . 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: ImageService.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 6 votes vote down vote up
/**
 * Pre-load some fake images
 *
 * @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
 */
@Bean
CommandLineRunner setUp() throws IOException {
	return (args) -> {
		FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));

		Files.createDirectory(Paths.get(UPLOAD_ROOT));

		FileCopyUtils.copy("Test file",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-cover.jpg"));

		FileCopyUtils.copy("Test file2",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-2nd-edition-cover.jpg"));

		FileCopyUtils.copy("Test file3",
			new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
	};
}
 
Example 2
Source File: ImageService.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 6 votes vote down vote up
/**
 * Pre-load some test images
 *
 * @return Spring Boot {@link CommandLineRunner} automatically
 *         run after app context is loaded.
 */
@Bean
CommandLineRunner setUp() throws IOException {
	return (args) -> {
		FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));

		Files.createDirectory(Paths.get(UPLOAD_ROOT));

		FileCopyUtils.copy("Test file",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-cover.jpg"));

		FileCopyUtils.copy("Test file2",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-2nd-edition-cover.jpg"));

		FileCopyUtils.copy("Test file3",
			new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
	};
}
 
Example 3
Source File: ImageService.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 6 votes vote down vote up
/**
 * Pre-load some fake images
 *
 * @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
 */
@Bean
CommandLineRunner setUp() throws IOException {
	return (args) -> {
		FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));

		Files.createDirectory(Paths.get(UPLOAD_ROOT));

		FileCopyUtils.copy("Test file",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-cover.jpg"));

		FileCopyUtils.copy("Test file2",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-2nd-edition-cover.jpg"));

		FileCopyUtils.copy("Test file3",
			new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
	};
}
 
Example 4
Source File: FetchingCacheServiceImpl.java    From genie with Apache License 2.0 6 votes vote down vote up
/**
 * Delete a resource version directory after taking appropriate lock.
 *
 * @param resourceVersionDir Directory to be deleted
 * @throws IOException
 */
private void cleanUpResourceVersion(final File resourceVersionDir)
    throws LockException, IOException {
    /*
     * Acquire a lock on the lock file for the resource version being deleted.
     * Delete the entire directory for the resource version
     */
    try (
        CloseableLock lock = fileLockFactory.getLock(
            touchCacheResourceVersionLockFile(resourceVersionDir)
        )
    ) {
        //critical section begin
        lock.lock();

        //Remove the data file. If last download was successful for the resource, only
        //data file would exist
        FileSystemUtils.deleteRecursively(getCacheResourceVersionDataFile(resourceVersionDir));

        //data.tmp file could exist if the last download of the resource failed in the middle
        //and after that a newer version was downloaded. So, delete it too
        FileSystemUtils.deleteRecursively(getCacheResourceVersionDownloadFile(resourceVersionDir));

        //critical section end
    }
}
 
Example 5
Source File: ImageService.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 6 votes vote down vote up
/**
 * Pre-load some test images
 *
 * @return Spring Boot {@link CommandLineRunner} automatically
 *         run after app context is loaded.
 */
@Bean
CommandLineRunner setUp() throws IOException {
	return (args) -> {
		FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));

		Files.createDirectory(Paths.get(UPLOAD_ROOT));

		FileCopyUtils.copy("Test file",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-cover.jpg"));

		FileCopyUtils.copy("Test file2",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-2nd-edition-cover.jpg"));

		FileCopyUtils.copy("Test file3",
			new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
	};
}
 
Example 6
Source File: ImageService.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 6 votes vote down vote up
/**
 * Pre-load some fake images
 *
 * @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
 */
@Bean
CommandLineRunner setUp() throws IOException {
	return (args) -> {
		FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));

		Files.createDirectory(Paths.get(UPLOAD_ROOT));

		FileCopyUtils.copy("Test file",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-cover.jpg"));

		FileCopyUtils.copy("Test file2",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-2nd-edition-cover.jpg"));

		FileCopyUtils.copy("Test file3",
			new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
	};
}
 
Example 7
Source File: ImageService.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 6 votes vote down vote up
/**
 * Pre-load some test images
 *
 * @return Spring Boot {@link CommandLineRunner} automatically
 *         run after app context is loaded.
 */
@Bean
CommandLineRunner setUp() throws IOException {
	return (args) -> {
		FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));

		Files.createDirectory(Paths.get(UPLOAD_ROOT));

		FileCopyUtils.copy("Test file",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-cover.jpg"));

		FileCopyUtils.copy("Test file2",
			new FileWriter(UPLOAD_ROOT +
				"/learning-spring-boot-2nd-edition-cover.jpg"));

		FileCopyUtils.copy("Test file3",
			new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
	};
}
 
Example 8
Source File: LocalFileManagerImpl.java    From entrada with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean rmdir(String location) {
  log.info("Delete local directory: " + location);

  File f = new File(location);
  if (f.exists() && f.isDirectory()) {
    return FileSystemUtils.deleteRecursively(f);
  }

  return false;
}
 
Example 9
Source File: HdfsTextWriterTests.java    From spring-cloud-task-app-starters with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
	tmpDir = System.getProperty("java.io.tmpdir") + "/jdbchdfs-task";
	File file = new File(tmpDir);
	if (file.exists()) {
		FileSystemUtils.deleteRecursively(file);
	}
	props = new JdbcHdfsTaskProperties();
	props.setFsUri("file:///");
	props.setDirectory(tmpDir);
	props.setFileName("dataWriterBasicTest");
}
 
Example 10
Source File: JavaDirectoryDeleteUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDirectory_whenDeletedWithSpringFileSystemUtils_thenIsGone() throws IOException {
    Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);

    boolean result = FileSystemUtils.deleteRecursively(pathToBeDeleted.toFile());

    assertTrue("Could not delete directory", result);
    assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
}
 
Example 11
Source File: FileSystemDropExporter.java    From mojito with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteDrop() throws DropExporterException {
    checkIsInitialized();

    Path deletePath = Paths.get(fileSystemDropExporterConfig.getDropFolderPath());
    boolean deleteRecursively = FileSystemUtils.deleteRecursively(deletePath.toFile());

    if (!deleteRecursively) {
        String msg = "Cannot delete the drop folder";
        logger.error(msg);
        throw new DropExporterException(msg);
    }
}
 
Example 12
Source File: ApplicationConfiguration.java    From logging-log4j-audit with Apache License 2.0 5 votes vote down vote up
@PreDestroy
public void destroy() {
    String tempDir = System.getProperty("java.io.tmpdir");
    gitLocalRepoPath = tempDir + "/audit/catalog";
    File file = new File(gitLocalRepoPath);
    FileSystemUtils.deleteRecursively(file);
}
 
Example 13
Source File: LocalTaskLauncherIntegrationTests.java    From spring-cloud-deployer-local with Apache License 2.0 5 votes vote down vote up
@Test
public void testInheritLoggingAndWorkDir() throws IOException {

	Map<String, String> appProperties = new HashMap();
	appProperties.put("killDelay", "0");
	appProperties.put("exitCode", "0");

	Map<String, String> deploymentProperties = new HashMap<>();
	deploymentProperties.put(LocalDeployerProperties.INHERIT_LOGGING, "true");
	Path tmpPath = new File(System.getProperty("java.io.tmpdir")).toPath();
	Path customWorkDirRoot = tmpPath.resolve("spring-cloud-deployer-task-workdir");
	deploymentProperties.put(LocalDeployerProperties.PREFIX + ".working-directories-root", customWorkDirRoot.toFile().getAbsolutePath());

	AppDefinition definition = new AppDefinition(this.randomName(), appProperties);

	List<Path> beforeDirs = new ArrayList<>();
	beforeDirs.add(customWorkDirRoot);
	if (Files.exists(customWorkDirRoot)) {
		beforeDirs = Files.walk(customWorkDirRoot, 1)
				.filter(path -> Files.isDirectory(path))
				.collect(Collectors.toList());
	}

	basicLaunchAndValidation(definition, deploymentProperties);
	assertTrue(this.outputCapture.toString().contains("Logs will be inherited."));

	List<Path> afterDirs = Files.walk(customWorkDirRoot, 1)
			.filter(path -> Files.isDirectory(path))
			.collect(Collectors.toList());
	assertThat("Additional working directory not created", afterDirs.size(), is(beforeDirs.size()+1));

	// clean up if test passed
	FileSystemUtils.deleteRecursively(customWorkDirRoot);
}
 
Example 14
Source File: FileSystemStorageService.java    From zhcet-web with Apache License 2.0 4 votes vote down vote up
@Override
public void deleteAll(FileType fileType) {
    FileSystemUtils.deleteRecursively(fromFileType(fileType).toFile());
}
 
Example 15
Source File: ConfigServerTestUtils.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
public static String copyLocalRepo(String path) throws IOException {
	File dest = new File(REPO_PREFIX + path);
	FileSystemUtils.deleteRecursively(dest);
	FileSystemUtils.copyRecursively(new File("target/repos/config-repo"), dest);
	return "file:./target/repos/" + path;
}
 
Example 16
Source File: SpringCloudCustomProjectDocumentationUpdater.java    From spring-cloud-release-tools with Apache License 2.0 4 votes vote down vote up
private void removeAFolderWithRedirection(File currentReleaseFolder) {
	if (!isSymbolinkLink(currentReleaseFolder)) {
		FileSystemUtils.deleteRecursively(currentReleaseFolder);
	}
}
 
Example 17
Source File: FileSystemStorageServiceImpl.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Override
public void deleteAll() {
    FileSystemUtils.deleteRecursively(rootLocation.toFile());
}
 
Example 18
Source File: FileSystemStorageService.java    From code-examples with MIT License 4 votes vote down vote up
@Override
public void deleteAll() {
    FileSystemUtils.deleteRecursively(rootLocation.toFile());
}
 
Example 19
Source File: ConfigServerTestUtils.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
public static boolean deleteLocalRepo(String path) throws IOException {
	File dest = new File(REPO_PREFIX + path);
	return FileSystemUtils.deleteRecursively(dest);
}
 
Example 20
Source File: UserModelService.java    From KOMORAN with Apache License 2.0 3 votes vote down vote up
public boolean deleteUserModel(String modelDir) {
    ModelValidator.CheckValidModelName(modelDir);

    String dirNameToDelete = String.join(File.separator, MODELS_BASEDIR, modelDir);

    return FileSystemUtils.deleteRecursively(new File(dirNameToDelete));
}