Java I/O How to - Get a File object containing the absolute path for the directory or file referenced by the current File object








Question

We would like to know how to get a File object containing the absolute path for the directory or file referenced by the current File object.

Answer

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

The code above generates the following result.

Returns the absolute path for the directory or file referenced by the current File object

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

The code above generates the following result.