Java I/O How to - Create Absolute path and relative path








Question

We would like to know how to create Absolute path and relative path.

Answer

import java.io.File;
/*from   w w w.  j a  va 2  s .  c  om*/
public class MainClass {

  public static void main(String[] args) {

    File absolute = new File("/public/html/javafaq/index.html");
    File relative = new File("html/javafaq/index.html");

    System.out.println("absolute: ");
    System.out.println(absolute.getName());
    System.out.println(absolute.getPath());

    System.out.println("relative: ");
    System.out.println(relative.getName());
    System.out.println(relative.getPath());
  }
}

The code above generates the following result.