Java IO Tutorial - Java Files.copy(Path source, OutputStream out)








Syntax

Files.copy(Path source, OutputStream out) has the following syntax.

public static long copy(Path source,  OutputStream out)   throws IOException

Example

In the following code shows how to use Files.copy(Path source, OutputStream out) method.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
//from   w  ww  .j  a va2  s .  co m
public class Main {

  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);
    }

  }
}

The code above generates the following result.