Copying a File - Java File Path IO

Java examples for File Path IO:File Copy

Description

Copying a File

Demo Code

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
  public static void main(String[] args) {
    FileSystem fileSystem = FileSystems.getDefault();
    System.out.println("FileSystem:" + fileSystem);
    Path sourcePath = fileSystem.getPath("file.log");
    Path targetPath = fileSystem.getPath("file2.log");
    System.out.println("Copy from " + sourcePath.toAbsolutePath().toString() 
       + " to " + targetPath.toAbsolutePath().toString());
    try {//from  w  ww.  j  a  v a2 s .c  om
      Files.copy(sourcePath, targetPath);
    } catch (IOException e) {
      e.printStackTrace();
    }

  }
}

Related Tutorials