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

Java 8Object Oriented ProgrammingProgramming

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());
raja
Published on 25-Feb-2019 07:18:45
Advertisements