File isAbsolute() method in Java with Examples
Last Updated :
30 Jan, 2019
The isAbsolute() method is a part of File class. The function returns whether the abstract pathname is absolute or not.
For Example: if we create a file object using the path as “program.txt”, it points to the file present in the same directory where the executable program is kept (if you are using a IDE it will point to the file where you have saved the program ). Here the path of the file mentioned above is “program.txt” but this path is not absolute (i.e. not complete) .Absolute path is the complete path from the root directory.
Function Signature:
public boolean isAbsolute()
Function Syntax:
file.isAbsolute()
Parameters: This function does not accept any parameters.
Return value: The function returns boolean value which tells whether the abstract pathname is absolute or not.
Below programs will illustrate the use of the isAbsolute() function
Example 1: We are given a file object of a file, we have to check whether it is absolute or not
import java.io.*;
public class solution {
public static void main(String args[])
{
try {
File f = new File( "c:\\users\\program.txt" );
if (f.isAbsolute()) {
System.out.println( "The path is absolute" );
}
else {
System.out.println( "The path is not absolute" );
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
|
Output:
The path is absolute
Example 2: We are given a file object of a file, we have to check whether it is absolute or not
import java.io.*;
public class solution {
public static void main(String args[])
{
try {
File f = new File( "program.txt" );
if (f.isAbsolute()) {
System.out.println( "The path is absolute" );
}
else {
System.out.println( "The path is not absolute" );
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
|
Output:
The path is not absolute
The programs might not run in an online IDE. please use an offline IDE and set the Parent file of the file
Share your thoughts in the comments
Please Login to comment...