Check if the File object refers to an absolute pathname in Java


The method java.io.File.isAbsolute() is used to check if the file object refers to an absolute pathname. This method returns true if the abstract path name is absolute and false if the abstract path name is not absolute.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.io.File;
public class Demo {
   public static void main(String[] args) {
      File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java");
      System.out.println(file.isAbsolute());
   }
}

The output of the above program is as follows −

Output

false

Now let us understand the above program.

The isAbsolute() method is used to check if the file object refers to an absolute pathname. The return value of the method is printed. A code snippet that demonstrates this is given as follows −

File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java");
System.out.println(file.isAbsolute());

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements