Moving a File - Java File Path IO

Java examples for File Path IO:File Move

Description

Moving 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();
    Path sourcePath = fileSystem.getPath("file.log");
    Path targetPath = fileSystem.getPath("file2.log");
    System.out.println("Move from " + sourcePath.toAbsolutePath().toString()
        + " to " + targetPath.toAbsolutePath().toString());
    try {//from  w ww.ja va 2 s  .c o  m
      Files.move(sourcePath, targetPath);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Result


Related Tutorials