Moving Files and Directories - Java File Path IO

Java examples for File Path IO:Directory Move

Introduction

options for Moving Files and Directories are listed as follows.

  • REPLACE_EXISTING: Replace the target. For symbolic link, the symbolic link is replaced but what it points to is not affected.
  • ATOMIC_MOVE: An atomic operation guarantees a complete file.

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 = FileSystems.getDefault().getPath(
        "C:/folder1/photos/test.jpg");

    try {/*from  w w  w .  j av a2 s. co  m*/
      Files.move(movefrom, moveto, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials