Example usage for java.nio.file Files deleteIfExists

List of usage examples for java.nio.file Files deleteIfExists

Introduction

In this page you can find the example usage for java.nio.file Files deleteIfExists.

Prototype

public static boolean deleteIfExists(Path path) throws IOException 

Source Link

Document

Deletes a file if it exists.

Usage

From source file:com.codealot.textstore.FileStore.java

@Override
public String storeText(final String text) throws IOException {
    Objects.requireNonNull(text, "No text provided");
    if (text.equals("")) {
        return this.storeText(NO_CONTENT);
    }//from   w  w w. j  a va  2 s  .co  m

    // make the digester
    final MessageDigest digester = getDigester();

    // make the hex hash
    final byte[] textBytes = text.getBytes(StandardCharsets.UTF_8);
    digester.update(textBytes);
    final String hash = byteToHex(digester.digest());

    // store the text
    final Path textPath = Paths.get(this.storeRoot, hash);
    if (!Files.exists(textPath)) {
        try {
            Files.write(textPath, textBytes);
        } catch (IOException e) {
            // make certain the file is not left on disk
            Files.deleteIfExists(textPath);
            // now re-throw the exception
            throw e;
        }
    }
    return hash;
}

From source file:wherehows.common.utils.JobsUtilTest.java

@Test
public void testGetEnabledJobs() throws IOException, ConfigurationException {
    String propertyStr1 = "job.class=test\n" + "job.cron.expr=0 0 1 * * ? *\n" + "#job.disabled=1\n"
            + "job.type=TEST1";
    String propertyStr2 = "job.class=test\n" + "#job.disabled=1\n";

    Path path1 = createPropertiesFile(propertyStr1);
    Path path2 = createPropertiesFile(propertyStr2);

    String filename1 = jobNameFromPath(path1);
    String filename2 = jobNameFromPath(path2);

    String dir = path1.getParent().toString();

    Map<String, Properties> jobs = getEnabledJobs(dir);

    Assert.assertEquals(jobs.size(), 2);
    Assert.assertEquals(jobs.get(filename1).getProperty("job.class"), "test");
    Assert.assertEquals(jobs.get(filename1).getProperty("job.disabled", ""), "");
    Assert.assertEquals(jobs.get(filename2).getProperty("job.class"), "test");

    Files.deleteIfExists(path1);
    Files.deleteIfExists(path2);/*  ww w  .  j  a v a  2s  .  c o m*/
}

From source file:at.ac.tuwien.ims.latex2mobiformulaconv.tests.integration.EndToEndConversionTest.java

@Before
public void setUp() throws Exception {
    workingDirectory = TestUtils.getWorkingDirectory(this.getClass());
    logger.info("Working directory: " + workingDirectory.toAbsolutePath().toString());

    converter.setWorkingDirectory(workingDirectory);
    title = RandomStringUtils.randomAlphanumeric(64);
    logger.debug("Random title: " + title);

    inputPaths = new ArrayList<>();
    inputPaths.add(workingDirectory.resolve(Paths.get(inputFilename)));

    outputPath = workingDirectory.resolve("..");
    outputFilePath = outputPath.resolve(filename + ext);
    Files.deleteIfExists(outputFilePath);
}

From source file:com.hpe.caf.worker.testing.preparation.PreparationResultProcessor.java

protected Path saveContentFile(TestItem<TInput, TExpected> testItem, String baseFileName, String extension,
        InputStream dataStream) throws IOException {
    String outputFolder = getOutputFolder();
    if (configuration.isStoreTestCaseWithInput()) {

        Path path = Paths.get(testItem.getInputData().getInputFile()).getParent();
        outputFolder = Paths.get(configuration.getTestDataFolder(), path == null ? "" : path.toString())
                .toString();//  ww w .  ja  v  a  2  s .c o m
    }

    baseFileName = FilenameUtils.normalize(baseFileName);
    baseFileName = Paths.get(baseFileName).getFileName().toString();
    Path contentFile = Paths.get(outputFolder, baseFileName + "." + extension + ".content");
    Files.deleteIfExists(contentFile);
    Files.copy(dataStream, contentFile, REPLACE_EXISTING);

    return getRelativeLocation(contentFile);
}

From source file:org.ow2.proactive_grid_cloud_portal.scheduler.WorkflowSubmitter.java

/**
 * Submits a workflow to the scheduler (XML or archive).
 * It also creates a job visualization HTML file.
 *
 * @param workflowFile a workflow file (XML or archive)
 * @param variables    variables to be replaced on submission
 * @return job ID of the job created for the specified workflow and associated variables.
 * @throws JobCreationRestException/*w  w w  . j a  v  a 2 s. c o  m*/
 * @throws NotConnectedRestException
 * @throws PermissionRestException
 * @throws SubmissionClosedRestException
 */
public JobId submit(File workflowFile, Map<String, String> variables) throws NotConnectedRestException,
        PermissionRestException, SubmissionClosedRestException, JobCreationRestException {
    try {
        Job job = createJobObject(workflowFile, variables);
        JobId jobId = scheduler.submit(job);
        // Create Job's SVG visualization file
        String visualization = job.getVisualization();
        if (visualization != null && !visualization.isEmpty()) {
            File visualizationFile = new File(PortalConfiguration.jobIdToPath(jobId.value()) + ".html");
            Files.deleteIfExists(visualizationFile.toPath());
            FileUtils.write(new File(visualizationFile.getAbsolutePath()), job.getVisualization(),
                    Charset.forName(FILE_ENCODING));
        }
        return jobId;
    } catch (NotConnectedException e) {
        throw new NotConnectedRestException(e);
    } catch (PermissionException e) {
        throw new PermissionRestException(e);
    } catch (SubmissionClosedException e) {
        throw new SubmissionClosedRestException(e);
    } catch (JobCreationException e) {
        throw new JobCreationRestException(e);
    } catch (Exception e) {
        logger.warn("Unexpected error when submitting job", e);
        throw new JobCreationRestException(e);
    }
}

From source file:org.digidoc4j.main.DigiDoc4JTest.java

@Test
public void signDDocContainerTwice() throws Exception {
    String fileName = "test1.bdoc";
    Files.deleteIfExists(Paths.get(fileName));

    String[] signNewContainerParams = new String[] { "-in", fileName, "-type", "DDOC", "-add",
            "testFiles/test.txt", "text/plain", "-pkcs12", "testFiles/signout.p12", "test" };
    String[] signExistingContainerParams = new String[] { "-in", fileName, "-pkcs12", "testFiles/signout.p12",
            "test" };

    callMainWithoutSystemExit(signNewContainerParams);
    callMainWithoutSystemExit(signExistingContainerParams);

    Container container = ContainerOpener.open(fileName);
    assertEquals(2, container.getSignatures().size());
}

From source file:at.ac.tuwien.ims.latex2mobiformulaconv.tests.integration.EndToEndConversionTest.java

@After
public void tearDown() throws Exception {
    Files.deleteIfExists(outputFilePath);
}

From source file:io.undertow.server.handlers.file.FileHandlerSymlinksTestCase.java

@After
public void deleteSymlinksScenario() throws IOException, URISyntaxException {
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();

    Path newSymlink = rootPath.resolve("newSymlink");
    Path newDir = rootPath.resolve("newDir");
    Path page = newDir.resolve("page.html");
    Path innerDir = newDir.resolve("innerDir");
    Path innerSymlink = newDir.resolve("innerSymlink");
    Path innerPage = innerDir.resolve("page.html");

    Files.deleteIfExists(innerSymlink);
    Files.deleteIfExists(newSymlink);
    Files.deleteIfExists(innerPage);
    Files.deleteIfExists(page);/*from www  .  j  ava 2s .c  o  m*/
    Files.deleteIfExists(innerDir);
    Files.deleteIfExists(newDir);
}

From source file:at.tfr.securefs.module.scan.IKScanServiceModule.java

@Override
public ModuleResult apply(InputStream is, ModuleConfiguration moduleConfiguration) throws ModuleException {

    Path filePath = configuration.getTmpPath().resolve("scanFile_" + UUID.randomUUID());
    try {//from  w  w w  .  j  a va2  s . c  o m
        try {
            Files.copy(is, filePath);
            return apply(filePath.toString(), moduleConfiguration);
        } finally {
            Files.deleteIfExists(filePath);
        }
    } catch (ModuleException me) {
        throw me;
    } catch (Exception e) {
        throw new ModuleException("cannot process", e);
    }
}

From source file:com.codealot.textstore.FileStore.java

@Override
public boolean deleteText(final String id) throws IOException {
    checkId(id);/* w w w  .ja va 2 s .  c  o  m*/
    final Path textPath = Paths.get(this.storeRoot, id);
    return Files.deleteIfExists(textPath);
}