Using the Path.resolve() method to move a file by extracting its name directly from the movefrom path - Java File Path IO

Java examples for File Path IO:Path

Description

Using the Path.resolve() method to move a file by extracting its name directly from the movefrom path

Demo Code

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

public class Main {
  public static void main(String[] args) throws Exception {
    Path movefrom = FileSystems.getDefault().getPath(
        "C:/folder1/test.jpg");
    Path moveto_dir = FileSystems.getDefault().getPath("C:/folder1/photos");

    try {//from  w w w. j a  v  a 2  s  .c  o  m
      Files.move(movefrom, moveto_dir.resolve(movefrom.getFileName()),
          StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials