Java I/O How to - Check if the File object refers to an absolute pathname








Question

We would like to know how to check if the File object refers to an absolute pathname.

Answer

isAbsolute() returns true if the File object refers to an absolute pathname, and false otherwise.


 
import java.io.File;
public class Main {
  public static void main(String[] a) {
    File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.separator, "File.java");
    System.out.println(myFile.isAbsolute());
  }
}

The code above generates the following result.