Convert a Path to a Real Path - Java File Path IO

Java examples for File Path IO:Path

Introduction

The toRealPath() method requires that the file must exist, which is not necessary if you use the toAbsolutePath() method.

If you want to ignore symbolic links, then pass to the method the LinkOption.NOFOLLOW_LINKS enum constant.

Demo Code

import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path path = Paths.get("/folder1/folder2/folder4", "test.txt");

    // convert path to "real" path
    try {//from  w w w  .j  a  v a 2 s. c o m
      Path real_path = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
      System.out.println("Path to real path: " + real_path);
    } catch (IOException e) {
      System.err.println(e);
    }
  }
}

Result


Related Tutorials