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(Path source, OutputStream out) throws IOException 

Source Link

Document

Copies all bytes from a file to an output stream.

Usage

From source file:br.unb.bionimbuz.storage.bucket.methods.CloudMethodsAmazonGoogle.java

@Override
public void StorageDownloadFile(BioBucket bucket, String bucketPath, String localPath, String fileName)
        throws Exception {

    switch (bucket.getProvider()) {
    case AMAZON: {

        s3client.setEndpoint(bucket.getEndPoint());
        S3Object object = s3client
                .getObject(new GetObjectRequest(bucket.getName(), bucketPath.substring(1) + fileName));
        InputStream objectData = object.getObjectContent();
        if (!(new File(localPath + fileName)).exists()) {
            Files.copy(objectData, Paths.get(localPath + fileName));
        }/*  ww w.j  ava  2  s  .co  m*/
        objectData.close();

        break;
    }
    case GOOGLE: {

        String command = gcloudFolder + "gsutil cp gs://" + bucket.getName() + bucketPath + fileName + " "
                + localPath + fileName;
        ExecCommand(command);

        break;
    }
    default: {
        throw new Exception("Provedor incorreto!");
    }
    }
}

From source file:com.github.dirkraft.jash.ShExecutableJarBundler.java

void concatJashAndJar() throws IOException {
    Preconditions.checkState(!_targetBinary.exists(), "Expected nothing to be here: %s",
            _targetBinary.getAbsolutePath());

    Files.copy(_renderedScriptFile.toPath(), _targetBinary.toPath());
    try (FileInputStream jarInputStream = new FileInputStream(jarFile);
            FileOutputStream appendJarStream = new FileOutputStream(_targetBinary, true)) {
        ByteStreams.copy(jarInputStream, appendJarStream);
        appendJarStream.flush();/*from   ww w  . j  a va2  s.com*/
    }

    JashIO.outf("Concatenated jash script and jar contents into %s%n", _targetBinary);
}

From source file:de.blizzy.backup.Utils.java

public static void zipFile(File source, File target) throws IOException {
    ZipOutputStream out = null;//  w w w  .j  a va 2 s  .  c om
    try {
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)));
        out.setLevel(Deflater.BEST_COMPRESSION);

        ZipEntry entry = new ZipEntry(source.getName());
        entry.setTime(source.lastModified());
        out.putNextEntry(entry);

        Files.copy(source.toPath(), out);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.gendai.modcreatorfx.template.ItemTemplate.java

/**
 * Set up all the directory used then for generating the java files.
 * Also get the texture file and the json({@link JSONObject}) file and modify as needed.
 *//*  ww  w . j a  v a2s. c  o  m*/
@SuppressWarnings("unchecked")
private void config() {
    javaDir = new File(Reference.OUTPUT_LOCATION + mod.getName() + "/java/" + mod.getName());
    javaDir.mkdirs();
    resDir = new File(
            Reference.OUTPUT_LOCATION + mod.getName() + "/resources/assets/" + mod.getName().toLowerCase());
    resDir.mkdirs();
    proxyDir = new File(javaDir + "/proxy");
    proxyDir.mkdirs();
    initDir = new File(javaDir + "/init");
    initDir.mkdirs();
    itemsDir = new File(javaDir + "/items");
    itemsDir.mkdirs();
    langDir = new File(resDir.getPath() + "/lang/en_US.lang");
    langDir.getParentFile().mkdirs();
    modelDir = new File(resDir.getPath() + "/models/item/" + item.getName().toLowerCase() + ".json");
    modelDir.getParentFile().mkdirs();
    try {
        langDir.createNewFile();
        FileWriter fw = new FileWriter(langDir.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("item." + item.getName() + ".name=" + item.getName());
        bw.close();
        //modeldir.createNewFile();
        Files.copy(
                Paths.get(Resource.class.getResource(item.getType().name() + ".json").getPath().substring(3)),
                Paths.get(modelDir.toString()));
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader(modelDir.toString()));
        JSONObject jsonObject = (JSONObject) obj;
        JSONObject arr = (JSONObject) jsonObject.get("textures");
        arr.put("layer0", mod.getName().toLowerCase() + ":items/" + item.getName().toLowerCase());
        FileWriter file = new FileWriter(modelDir.toString());
        String rep = jsonObject.toString().replace("\\/", "/");
        file.write(rep);
        file.flush();
        file.close();
    } catch (IOException | ParseException e1) {
        e1.printStackTrace();
    }
    res = item.getTexturefile();
    Path pathto = Paths.get(resDir.getPath() + "/textures/items/" + item.getName().toLowerCase() + ".png");
    pathto.toFile().getParentFile().mkdirs();
    try {
        Files.copy(res.toPath(), pathto);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:at.tfr.securefs.process.ProcessFilesTest.java

@Test(expected = SecureFSError.class)
public void overwriteExistingFileProhibited() throws Exception {
    // Given: the target directory AND file(!), the source file
    toFilesPath = Files.createDirectories(toRoot.resolve(DATA_FILES));
    final String data = "Hallo Echo";
    try (OutputStream os = cp.getEncrypter(fromFile)) {
        IOUtils.write(data, os);/* w  w  w .  j  a v a  2s .  c  om*/
    }
    Files.copy(fromFile, targetToFile);
    Assert.assertTrue(Files.exists(targetToFile));

    ProcessFilesData cfd = new ProcessFilesData().setFromRootPath(fromRoot.toString())
            .setToRootPath(toRoot.toString()).setUpdate(false).setProcessActive(true);

    // When: copy of source file to toRoot
    ProcessFilesBean pf = new ProcessFilesBean(new MockSecureFsCache());
    pf.copy(fromFile, fromRoot.getNameCount(), toRoot, cp, newSecret, cfd);

    // Then: Exception overwrite not allowed!!
}

From source file:br.unb.cic.bionimbuz.services.storage.bucket.methods.CloudMethodsAmazonGoogle.java

@Override
public void StorageDownloadFile(BioBucket bucket, String bucketPath, String localPath, String fileName)
        throws Exception {

    switch (bucket.getProvider()) {
    case AMAZON: {

        s3client.setEndpoint(bucket.getEndPoint());
        final S3Object object = s3client
                .getObject(new GetObjectRequest(bucket.getName(), bucketPath.substring(1) + fileName));
        final InputStream objectData = object.getObjectContent();
        Files.copy(objectData, Paths.get(localPath + fileName));
        objectData.close();/* w  w w  . ja v a  2s  .  com*/

        break;
    }
    case GOOGLE: {

        final String command = gcloudFolder + "gsutil cp gs://" + bucket.getName() + bucketPath + fileName + " "
                + localPath + fileName;
        ExecCommand(command);

        break;
    }
    default: {
        throw new Exception("Provedor incorreto!");
    }
    }
}

From source file:org.apache.flink.tests.util.FlinkDistribution.java

@Override
public void before() throws IOException {
    defaultConfig = new UnmodifiableConfiguration(
            GlobalConfiguration.loadConfiguration(conf.toAbsolutePath().toString()));
    final Path originalConfig = conf.resolve(FLINK_CONF_YAML);
    final Path backupConfig = conf.resolve(FLINK_CONF_YAML_BACKUP);
    Files.copy(originalConfig, backupConfig);
    filesToDelete.add(new AutoClosablePath(backupConfig));
}

From source file:com.stratio.mojo.scala.crossbuild.RewritePomTest.java

/**
 * Regression test for https://github.com/Stratio/scala-cross-build-maven-plugin/issues/23
 * @throws IOException/*from   w w w .ja  v a 2 s .c  om*/
 * @throws XMLStreamException
 */
@Test
public void rewriteWithoutStackOverflow_Issue32() throws IOException, XMLStreamException {
    final RewritePom rewritePom = new RewritePom();
    tempDir.create();
    final File file = tempDir.newFile();
    file.delete();
    Files.copy(getClass().getResourceAsStream("/issue_23_pom.xml"), file.toPath());
    final MavenProject project = getMockMavenProject(file);
    final String newBinaryVersion = "2.10";
    final String newVersion = "2.10.4";
    rewritePom.rewrite(project, "scala.binary.version", "scala.version", newBinaryVersion, newVersion);
    assertEqualToResource(file, "/issue_23_pom_result.xml");
    file.delete();
}

From source file:com.thoughtworks.go.plugin.infra.commons.PluginsZip.java

public void create() {
    checkFilesAccessibility(bundledPlugins, externalPlugins);
    reset();//w  w w. ja  v  a  2 s. c o m

    MessageDigest md5Digest = DigestUtils.getMd5Digest();
    try (ZipOutputStream zos = new ZipOutputStream(
            new DigestOutputStream(new BufferedOutputStream(new FileOutputStream(destZipFile)), md5Digest))) {
        for (GoPluginDescriptor agentPlugins : agentPlugins()) {
            String zipEntryPrefix = "external/";

            if (agentPlugins.isBundledPlugin()) {
                zipEntryPrefix = "bundled/";
            }

            zos.putNextEntry(
                    new ZipEntry(zipEntryPrefix + new File(agentPlugins.pluginFileLocation()).getName()));
            Files.copy(new File(agentPlugins.pluginFileLocation()).toPath(), zos);
            zos.closeEntry();
        }
    } catch (Exception e) {
        LOG.error("Could not create zip of plugins for agent to download.", e);
    }

    md5DigestOfPlugins = Hex.encodeHexString(md5Digest.digest());
}

From source file:com.google.jenkins.plugins.persistentmaster.volume.zip.ZipCreator.java

private void copyRegularFile(Path file, String filenameInZip) throws IOException {
    logger.finer("Adding file: " + file + " with filename: " + filenameInZip);
    ZipArchiveEntry entry = new ZipArchiveEntry(filenameInZip);
    zipStream.putArchiveEntry(entry);/*from w  w  w.  jav  a  2 s  .  com*/
    Files.copy(file, zipStream);
    zipStream.closeArchiveEntry();
}