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:Test.java

public static void main(String[] args) throws Exception {
    Path originalDirectory = FileSystems.getDefault().getPath("C:/home/docs");
    Path newDirectory = FileSystems.getDefault().getPath("C:/home/tmp");

    Files.copy(originalDirectory, newDirectory);
    System.out.println("Directory copied successfully!");

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path sourceFile = FileSystems.getDefault().getPath("C:/home/docs/users.txt");
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Files.copy(sourceFile, outputStream);
    byte arr[] = outputStream.toByteArray();
    System.out.println("The contents of " + sourceFile.getFileName());
    for (byte data : arr) {
        System.out.print((char) data);
    }/*from www .j  a  v  a  2s .c  o  m*/

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path newFile = FileSystems.getDefault().getPath("C:/h.html");
    URI url = URI.create("http://jdk7.java.net/");
    InputStream inputStream = url.toURL().openStream();
    Files.copy(inputStream, newFile);
    System.out.println("Site copied successfully!");

}

From source file:Main.java

public static void main(String[] args) {

    Path copy_from_4 = Paths.get("C:/tutorial/Java/JavaFX", "tutor.txt");
    Path copy_to_4 = Paths.get("C:/tutorial/Java/Swing", "tutor.txt");
    try (OutputStream os = new FileOutputStream(copy_to_4.toFile())) {

        Files.copy(copy_from_4, os);

    } catch (IOException e) {
        System.err.println(e);/*w  w  w .  j  ava 2  s .c  o m*/
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path newFile = FileSystems.getDefault().getPath("C:/home/docs/newFile.txt");
    Path copiedFile = FileSystems.getDefault().getPath("C:/home/docs/copiedFile.txt");

    Files.createFile(newFile);//  w w w . j  av  a2s. c o m
    System.out.println("File created successfully!");
    Files.copy(newFile, copiedFile);
    System.out.println("File copied successfully!");
    Files.copy(newFile, copiedFile, StandardCopyOption.REPLACE_EXISTING);

}

From source file:Main.java

public static void backupComputationExperimentResults(String appendix) throws IOException {

    String name = COMPEXP_LOG_PATH.replace(".xml", "-" + appendix + ".xml");

    while ((new File(name)).exists()) {
        name = name.replace(".xml", "-" + appendix + ".xml");
    }/*from w  ww  .  j av a2  s. c  o m*/
    Files.copy(new File(COMPEXP_LOG_PATH).toPath(), new File(name).toPath());
}

From source file:gob.dp.simco.comun.FunctionUtil.java

public static String uploadArchive(Part fil) {
    String nameArchive = getFilename(fil);
    String extencion = getFileExtension(getFilename(fil));
    if (StringUtils.isNoneBlank(nameArchive)) {
        String formato = RandomStringUtils.random(32, 0, 20, true, true, "qw32rfHIJk9iQ8Ud7h0X".toCharArray());
        String ruta = formato + extencion;
        File file = new File(ConstantesUtil.FILE_SYSTEM + ruta);
        try (InputStream input = fil.getInputStream()) {
            Files.copy(input, file.toPath());
        } catch (IOException ex) {
            log.error(ex);//w  w  w. j  a va2s  .co m
        }
        return ruta;
    }
    return null;
}

From source file:jease.site.Streams.java

/**
 * Write given file to response./*w  w w. j  av  a  2 s .c  o  m*/
 * 
 * If the given content type denotes a browser supported image, the image
 * will be automatically scaled if either "scale" is present as request
 * paramter or JEASE_IMAGE_LIMIT is set in Registry.
 */
public static void write(HttpServletRequest request, HttpServletResponse response, File file,
        String contentType) throws IOException {
    if (Images.isBrowserCompatible(contentType)) {
        int scale = NumberUtils.toInt(request.getParameter("scale"));
        if (scale > 0) {
            java.io.File scaledImage = Images.scale(file, scale);
            scaledImage.deleteOnExit();
            response.setContentType(contentType);
            response.setContentLength((int) scaledImage.length());
            Files.copy(scaledImage.toPath(), response.getOutputStream());
            return;
        }
        int limit = NumberUtils.toInt(Registry.getParameter(Names.JEASE_IMAGE_LIMIT));
        if (limit > 0) {
            java.io.File scaledImage = Images.limit(file, limit);
            scaledImage.deleteOnExit();
            response.setContentType(contentType);
            response.setContentLength((int) scaledImage.length());
            Files.copy(scaledImage.toPath(), response.getOutputStream());
            return;
        }

    }
    response.setContentType(contentType);
    response.setContentLength((int) file.length());
    Files.copy(file.toPath(), response.getOutputStream());
}

From source file:muffinc.yafdivj.helper.FeretHandler.java

public static void move(File file) {
    try {//from   www. j  a  va2s  . c om

        String newFolder = NEW_FOLDER + file.getName().substring(1, 5) + "/";
        File file1 = new File(newFolder);

        if (!file1.exists()) {
            file1.mkdirs();
        }

        Files.copy(file.toPath(), new File(newFolder + file.getName()).toPath());

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Test.java

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
    System.out.println("Copying " + source.relativize(file));
    Files.copy(file, target.resolve(source.relativize(file)));
    return FileVisitResult.CONTINUE;
}