Example usage for java.nio.file Files copy

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

Introduction

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

Prototype

public static long copy(InputStream in, Path target, CopyOption... options) throws IOException 

Source Link

Document

Copies all bytes from an input stream to a file.

Usage

From source file:com.excelsiorjet.api.util.Utils.java

public static void copyFile(Path source, Path target) throws IOException {
    if (!target.toFile().exists()) {
        Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES);
    } else if (source.toFile().lastModified() != target.toFile().lastModified()) {
        //copy only files that were changed
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
    }/*  ww w.jav  a  2 s .  c  o m*/

}

From source file:ninja.uploads.DiskFileItemProvider.java

@Override
public FileItem create(FileItemStream item) {

    File tmpFile = null;/*from w  w  w. ja v a 2s .c  o m*/

    // do copy
    try (InputStream is = item.openStream()) {
        tmpFile = File.createTempFile("nju", null, tmpFolder);
        Files.copy(is, tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        throw new RuntimeException("Failed to create temporary uploaded file on disk", e);
    }

    // return
    final String name = item.getName();
    final File file = tmpFile;
    final String contentType = item.getContentType();
    final FileItemHeaders headers = item.getHeaders();

    return new FileItem() {
        @Override
        public String getFileName() {
            return name;
        }

        @Override
        public InputStream getInputStream() {
            try {
                return new FileInputStream(file);
            } catch (FileNotFoundException e) {
                throw new RuntimeException("Failed to read temporary uploaded file from disk", e);
            }
        }

        @Override
        public File getFile() {
            return file;
        }

        @Override
        public String getContentType() {
            return contentType;
        }

        @Override
        public FileItemHeaders getHeaders() {
            return headers;
        }

        @Override
        public void cleanup() {
            // try to delete temporary file, silently fail on error
            try {
                file.delete();
            } catch (Exception e) {
            }
        }
    };

}

From source file:org.wte4j.examples.showcase.server.hsql.ShowCaseDbInitializer.java

void createDateBaseFiles(Path dataBaseLocation, boolean overide) {
    Path hsqlScript = dataBaseLocation.resolve("wte4j-showcase.script");
    if (overide || !Files.exists(hsqlScript)) {
        try {/*from w  w  w . ja v a2 s  .  c  o m*/
            if (!Files.exists(dataBaseLocation)) {
                Files.createDirectories(dataBaseLocation);
            }
            Resource[] resources = resourceLoader.getResources("classpath:/db/*");
            for (Resource resource : resources) {
                Path filePath = dataBaseLocation.resolve(resource.getFilename());
                File source = resource.getFile();
                Files.copy(source.toPath(), filePath, StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("can not copy database files", e);
        }
    }
}

From source file:mx.eersya.beans.CreateItemBean.java

public void savePicture() {
    try {//from   w  w  w .  j  a v  a2 s  . com
        Path path = Paths.get("/home/eersya/Projects/W/");
        String filename = FilenameUtils.getBaseName(picture.getFileName());
        String extension = FilenameUtils.getExtension(picture.getFileName());
        Path file = Files.createTempFile(path, filename, "." + extension);
        picturePath = file.toString();
        try (InputStream in = picture.getInputstream()) {
            Files.copy(in, file, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException exa) {
            System.err.println(exa);
        }
        picturePath = file.toString();
        System.out.println("Picture:" + picturePath);
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:com.netflix.nicobar.core.persistence.ArchiveRepositoryTest.java

@BeforeClass
public void setup() throws Exception {
    URL testJarUrl = getClass().getClassLoader().getResource(TEST_MODULE_SPEC_JAR.getResourcePath());
    if (testJarUrl == null) {
        fail("Couldn't locate " + TEST_MODULE_SPEC_JAR.getResourcePath());
    }/*  ww  w .  j a  v  a  2 s.  c o m*/
    testArchiveJarFile = Files.createTempFile(TEST_MODULE_SPEC_JAR.getModuleId().toString(), ".jar");
    InputStream inputStream = testJarUrl.openStream();
    Files.copy(inputStream, testArchiveJarFile, StandardCopyOption.REPLACE_EXISTING);
    IOUtils.closeQuietly(inputStream);
}

From source file:com.ejisto.util.visitor.ConditionMatchingCopyFileVisitor.java

private void copy(Path srcFile) throws IOException {
    Path relative = options.srcRoot.relativize(srcFile);
    int count = relative.getNameCount();
    StringBuilder newFileName = new StringBuilder();
    if (count > 1) {
        newFileName.append(relative.getParent().toString());
        newFileName.append(File.separator);
    }//from www . j a va  2  s  .  co m
    newFileName.append(defaultString(options.filesPrefix)).append(srcFile.getFileName().toString());
    Files.copy(srcFile, options.targetRoot.resolve(newFileName.toString()),
            StandardCopyOption.REPLACE_EXISTING);
}

From source file:org.apache.hadoop.hbase.client.TestUpdateConfiguration.java

@Test
public void testMasterOnlineConfigChange() throws IOException {
    LOG.debug("Starting the test");
    Path cnfPath = FileSystems.getDefault().getPath("target/test-classes/hbase-site.xml");
    Path cnf2Path = FileSystems.getDefault().getPath("target/test-classes/hbase-site2.xml");
    Path cnf3Path = FileSystems.getDefault().getPath("target/test-classes/hbase-site3.xml");
    // make a backup of hbase-site.xml
    Files.copy(cnfPath, cnf3Path, StandardCopyOption.REPLACE_EXISTING);
    // update hbase-site.xml by overwriting it
    Files.copy(cnf2Path, cnfPath, StandardCopyOption.REPLACE_EXISTING);

    Admin admin = TEST_UTIL.getHBaseAdmin();
    ServerName server = TEST_UTIL.getHBaseCluster().getMaster().getServerName();
    admin.updateConfiguration(server);/*from  www  .  j a  v a 2s.com*/
    Configuration conf = TEST_UTIL.getMiniHBaseCluster().getMaster().getConfiguration();
    int custom = conf.getInt("hbase.custom.config", 0);
    assertEquals(custom, 1000);
    // restore hbase-site.xml
    Files.copy(cnf3Path, cnfPath, StandardCopyOption.REPLACE_EXISTING);
}

From source file:io.fabric8.profiles.Profiles.java

/**
 * @param target       is the directory where resulting materialized profile configuration will be written to.
 * @param profileNames a list of profile names that will be combined to create the materialized profile.
 *//*from w ww.j a  v  a 2s .co m*/
public void materialize(Path target, String... profileNames) throws IOException {
    ArrayList<String> profileSearchOrder = new ArrayList<>();
    for (String profileName : profileNames) {
        collectProfileNames(profileSearchOrder, profileName);
    }

    HashSet<String> files = new HashSet<>();
    for (String profileName : profileSearchOrder) {
        files.addAll(listFiles(profileName));
    }

    System.out.println("profile search order" + profileSearchOrder);
    System.out.println("files: " + files);
    for (String file : files) {
        try (InputStream is = materializeFile(file, profileSearchOrder)) {
            Files.copy(is, target.resolve(file), StandardCopyOption.REPLACE_EXISTING);
        }
    }

}

From source file:cz.etnetera.reesmo.writer.storage.FileSystemStorage.java

@Override
protected Result createResult(String projectKey, Result result, List<Object> attachments)
        throws StorageException {
    File baseDir = this.baseDir;
    File resultDir = createModelDir(baseDir, result);

    createModelFile(resultDir, result);/*w w  w  .  jav  a  2 s  .  co  m*/
    if (projectKey != null)
        createModelProjectKeyFile(resultDir, result, projectKey);

    if (attachments != null && !attachments.isEmpty()) {
        File resultAttachmentDir = createResultAttachmentDir(resultDir);
        for (Object attachment : attachments) {
            if (attachment instanceof File) {
                try {
                    Files.copy(((File) attachment).toPath(), resultAttachmentDir.toPath(),
                            StandardCopyOption.REPLACE_EXISTING);
                } catch (IOException e) {
                    throw new StorageException("Unable to copy result attachment file: " + attachment, e);
                }
            } else if (attachment instanceof ExtendedFile) {
                ExtendedFile file = (ExtendedFile) attachment;
                File targetFile = new File(resultAttachmentDir, file.getPath());
                targetFile.mkdirs();
                try {
                    Files.copy(file.getFile().toPath(), targetFile.toPath(),
                            StandardCopyOption.REPLACE_EXISTING);
                } catch (IOException e) {
                    throw new StorageException("Unable to copy result attachment file: " + attachment, e);
                }
            } else {
                throw new StorageException("Unsupported attachment type: " + attachment.getClass());
            }
        }
    }

    createModelReadyFile(resultDir, result);
    result.setId(createModelId(resultDir));

    return result;
}

From source file:io.github.tsabirgaliev.ZipperInputStreamTest.java

public void testFileSystemOutput() throws IOException {
    ZipperInputStream lzis = new ZipperInputStream(enumerate(file1, file2));

    Files.copy(lzis, Paths.get("target", "output.zip"), StandardCopyOption.REPLACE_EXISTING);
}