Java I/O How to - Convert a relative path into an absolute path








Question

We would like to know how to convert a relative path into an absolute path.

Answer

import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
/*from   www .ja  v a 2 s.co  m*/
public class Main {

  public static void main(String[] args) throws Exception {
    Path path = Paths.get("/home", "docs", "users.txt");
    System.out.println("Absolute path: " + path.toAbsolutePath());


  }
}

The code above generates the following result.