Example usage for java.nio.file StandardCopyOption REPLACE_EXISTING

List of usage examples for java.nio.file StandardCopyOption REPLACE_EXISTING

Introduction

In this page you can find the example usage for java.nio.file StandardCopyOption REPLACE_EXISTING.

Prototype

StandardCopyOption REPLACE_EXISTING

To view the source code for java.nio.file StandardCopyOption REPLACE_EXISTING.

Click Source Link

Document

Replace an existing file if it exists.

Usage

From source file:system.basic.CLIBasicTests.java

@BeforeClass
public static void saveWskProps() throws IOException {
    if (wskProps.exists()) {
        tmpProps = File.createTempFile("wskProps", ".tmp");
        Files.copy(wskProps.toPath(), tmpProps.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }//from   w w w .ja va 2s. c o m
}

From source file:de.douglas.maven.plugin.rpmsystemd.systemd.SystemdMojoTest.java

public void testGenerateSystemdFileWithDefaultSettings() throws Exception {
    String testPomName = "pom-defaults.xml";

    Path testBaseDir = createDir(systemdTestsBaseDir.resolve("defaults"));
    Path pomPath = testBaseDir.resolve(testPomName);

    Path srcPom = systemdSrcDir.resolve(testPomName);
    Files.copy(srcPom, pomPath, StandardCopyOption.REPLACE_EXISTING);

    createDir(SystemPaths$.MODULE$.generationRootDir(testBaseDir));

    SystemdMojo systemdMojo = (SystemdMojo) lookupMojo(pomPath, "generate-systemd-file");
    systemdMojo.execute();// w  w  w  .  j  av  a  2  s.  c  o m

    assertTrue("Generated systemd service file does not equal expected one",
            FileUtils.contentEquals(systemdSrcDir.resolve("test-module.service-defaults-expected").toFile(),
                    testBaseDir.resolve("target").resolve("rpm-systemd-maven-plugin")
                            .resolve("test-module.service").toFile()));
}

From source file:de.douglas.maven.plugin.rpmsystemd.rpm.PostInstMojoTest.java

public void testGenerateRpmPostInstFileWithDefaultSettings() throws Exception {
    String testPomName = "pom-defaults.xml";

    Path testBaseDir = createDir(postInstTestsBaseDir.resolve("defaults"));
    Path pomPath = testBaseDir.resolve(testPomName);

    Path srcPom = postInstSrcDir.resolve(testPomName);
    Files.copy(srcPom, pomPath, StandardCopyOption.REPLACE_EXISTING);

    createDir(SystemPaths$.MODULE$.generationRootDir(testBaseDir));

    PostInstMojo postInstMojo = (PostInstMojo) lookupMojo(pomPath, "generate-rpm-postinst-file");
    postInstMojo.execute();/* www.  j  a  v a2 s  . c  om*/

    assertTrue("Generated postinst file does not equal expected one", FileUtils.contentEquals(
            postInstSrcDir.resolve("postinst-defaults-expected").toFile(),
            testBaseDir.resolve("target").resolve("rpm-systemd-maven-plugin").resolve("postinst").toFile()));
}

From source file:com.facebook.buck.step.fs.ZstdStepTest.java

@Test
public void testZstdStep() throws Exception {
    Path sourceFileOriginal = TestDataHelper.getTestDataScenario(this, "compression_test").resolve("step.data");
    Path sourceFile = tmp.newFile("step.data").toPath();
    Files.copy(sourceFileOriginal, sourceFile, StandardCopyOption.REPLACE_EXISTING);
    File destinationFile = tmp.newFile("step.data.zstd");

    ZstdStep step = new ZstdStep(TestProjectFilesystems.createProjectFilesystem(tmp.getRoot().toPath()),
            sourceFile, destinationFile.toPath());

    ExecutionContext context = TestExecutionContext.newInstance();

    assertEquals(0, step.execute(context).getExitCode());

    ByteSource original = PathByteSource.asByteSource(sourceFileOriginal);
    ByteSource decompressed = new ByteSource() {
        @Override//from   w  ww .j  a va2  s . c o  m
        public InputStream openStream() throws IOException {
            return new ZstdCompressorInputStream(new FileInputStream(destinationFile));
        }
    };

    assertFalse(Files.exists(sourceFile));
    assertTrue("Decompressed file must be identical to original.", original.contentEquals(decompressed));
}

From source file:update.moveUpdateItem.java

@Override
public boolean execute() {
    Path source = Paths.get(sourceDir.getAbsolutePath(), relativeSourceDir, super.getID());
    Path dest = Paths.get(System.getProperty("user.dir"), destDir);
    System.out.println(source);/*from ww  w .  ja va  2s.  c  o  m*/
    System.out.println(dest.getParent());

    if (!source.toFile().exists()) {
        System.err.println("NO file to move!");
        return false;
    }
    if (!dest.getParent().toFile().exists()) {
        if (!dest.getParent().toFile().mkdirs()) {
            System.err.println("NO dest directory");
            return false;
        }
    }

    if (source.toFile().isDirectory()) {
        try {
            FileUtils.copyDirectory(source.toFile(), dest.toFile(), true);
            return true;
        } catch (IOException e) {
            System.err.println(e);
            return false;
        }
    }
    try {
        Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        System.err.println(e);
        return false;
    }
    return true;
}

From source file:nl.tudelft.graphalytics.granula.logging.GangliaLogger.java

private static void copyFolder(File sourceFolder, File destinationFolder) throws IOException {
    if (sourceFolder.isDirectory()) {
        if (!destinationFolder.exists()) {
            destinationFolder.mkdir();//from ww  w .  ja  v a2 s .c om
        }

        //Get all files from source directory
        String files[] = sourceFolder.list();

        //Iterate over all files and copy them to destinationFolder one by one
        for (String file : files) {
            File srcFile = new File(sourceFolder, file);
            File destFile = new File(destinationFolder, file);

            //Recursive function call
            copyFolder(srcFile, destFile);
        }
    } else {
        Files.copy(sourceFolder.toPath(), destinationFolder.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }
}

From source file:system.basic.CLIBasicTests.java

@AfterClass
public static void restoreWskProps() throws IOException {
    if (tmpProps != null && tmpProps.exists()) {
        Files.copy(tmpProps.toPath(), wskProps.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }//from   w ww  .j  a v a 2 s  .co m
}

From source file:com.javacreed.api.secureproperties.spring.SpringPropertiesTest.java

@Test
public void test() throws IOException {

    final String[] configurations = { "test-configuration-1.xml", "test-configuration-2.xml",
            "test-configuration-3.xml", "test-configuration-4.xml", "test-configuration-5.xml",
            /* "test-configuration-6.xml" */ };

    /*/*from w  w w . j a v  a2 s. c  o m*/
     * The source properties file that contains the plain text password and the target properties file from where the
     * configuration reads the properties
     */
    final File source = new File("src/test/resources/samples/properties/file.001.properties");
    final File target = new File("target/samples/properties/file.001.properties");
    target.getParentFile().mkdirs();

    for (final String configuration : configurations) {

        // Copy the properties file before the test
        Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);

        for (int i = 0; i < 3; i++) {
            try (ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                    "/spring/" + configuration)) {
                // Check all three properties
                Assert.assertEquals("Albert",
                        applicationContext.getBeanFactory().resolveEmbeddedValue("${name}"));
                Assert.assertEquals("Somewhere in Malta",
                        applicationContext.getBeanFactory().resolveEmbeddedValue("${address}"));
                Assert.assertEquals("my long secret password",
                        applicationContext.getBeanFactory().resolveEmbeddedValue("${password}"));

                // Make sure that the password was encoded
                final List<String> lines = Files.readAllLines(target.toPath(), Charset.forName("UTF-8"));
                Assert.assertEquals(4, lines.size());
                Assert.assertEquals("# This is a simple properties file", lines.get(0));
                Assert.assertEquals("name=Albert", lines.get(1));
                Assert.assertEquals("address=Somewhere in Malta", lines.get(2));
                Assert.assertTrue(lines.get(3).startsWith("password={enc}"));
                Assert.assertFalse(lines.get(3).contains("my long secret password"));
            }
        }
    }
}

From source file:org.artificer.repository.hibernate.file.FilesystemFileManager.java

@Override
public void write(ArtificerDocumentArtifact artifact, ArtifactContent content, EntityManager entityManager)
        throws Exception {
    InputStream inputStream = null;
    try {/*from  w  w  w. ja va 2 s . c o m*/
        File out = new File(path + artifact.getUuid());
        out.createNewFile();
        inputStream = content.getInputStream();
        Files.copy(content.getInputStream(), out.toPath(), StandardCopyOption.REPLACE_EXISTING);
        artifact.setContentPath(path + artifact.getUuid());
    } finally {
        if (inputStream != null) {
            IOUtils.closeQuietly(inputStream);
        }
    }
}

From source file:io.lavagna.service.ExportImportServiceTest.java

@Test
public void testImportAndExport() throws IOException {
    Path tmp = Files.createTempFile(null, null);
    try (InputStream is = new ClassPathResource("io/lavagna/export2.zip").getInputStream()) {
        Files.copy(is, tmp, StandardCopyOption.REPLACE_EXISTING);
        exportImportService.importData(false, tmp);

        // TODO additional checks

        exportImportService.exportData(new ByteArrayOutputStream());
    } finally {//  w ww  .j  a v  a 2 s  . c o m
        if (tmp != null) {
            Files.deleteIfExists(tmp);
        }
    }

}