The Wayback Machine - https://web.archive.org/web/20240619215448/https://www.geeksforgeeks.org/file-isfile-method-in-java-with-examples/
Open In App

File isFile() method in Java with Examples

Last Updated : 28 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The isFile() function is a part of File class in Java. This function determines whether the is a file or Directory denoted by the abstract filename is File or not. The function returns true if the abstract file path is File else returns false.

Function signature:

public boolean isFile()

Syntax:

file.isFile()

Parameters: This method does not accept any parameter.

Return Type The function returns boolean data type representing whether the abstract file path is file or not

Exception: This method throws Security Exception if the write access to the file is denied

Below programs illustrates the use of isFile() function:

Example 1: The file “F:\\program.txt” is a existing file in F: directory.




// Java program to demonstrate
// isFile() method of File Class
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // Get the file
        File f = new File("F:\\program.txt");
  
        // Check if the specified file
        // is File or not
        if (f.isFile())
            System.out.println("File");
        else
            System.out.println("Not a File");
    }
}


Output:

File

Example 2: The file “F:\\program” is a directory




// Java program to demonstrate
// isFile() method of File Class
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // Get the file
        File f = new File("F:\\program");
  
        // Check if the specified file
        // is File or not
        if (f.isFile())
            System.out.println("File");
        else
            System.out.println("Not a File");
    }
}


Output:

Not a File

Note: The programs might not run in an online IDE. Please use an offline IDE and set the path of the file.



Similar Reads

File getAbsolutePath() method in Java with Examples
The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object.If the pathname of the file object is absolute then it simply returns the path of the current file object. For Example: if we create a file object using the path as "program.txt", it points to the file present in the same direc
3 min read
File getAbsoluteFile() method in Java with Examples
The getAbsoluteFile() method is a part of File class. This function returns the absolute File object of the given abstract pathname. The absolute file or directory points to the same file or directory as the given File object. For Example: If we create a file object using the path as "program.txt", it points to the file present in the same director
3 min read
File canExecute() method in Java with Examples
The canExecute() function is a part of File class in Java. This function determines whether the program can execute the specified file denoted by the abstract pathname. If the file path exists and the application is allowed to execute the file, this method will return true. Else it will return false. Function signature: public boolean canExecute()
2 min read
File isDirectory() method in Java with Examples
The isDirectory() function is a part of File class in Java . This function determines whether the is a file or directory denoted by the abstract filename is Directory or not.The function returns true if the abstract file path is Directory else returns false. Function signature: public boolean isDirectory() Syntax: file.isDirectory() Parameters: Thi
2 min read
File canRead() method in Java with Examples
The canRead()function is a part of the File class in Java. This function determines whether the program can read the file denoted by the abstract pathname. The function returns true if the abstract file path exists and the application is allowed to read the file.Function signature: public boolean canRead() Syntax: file.canRead() Parameters: This me
2 min read
File canWrite() method in Java with examples
The canWrite()function is a part of File class in Java . This function determines whether the program can write the file denoted by the abstract path name.The function returns true if the abstract file path exists and the application is allowed to write the file. Function signature: public boolean canWrite() Syntax: file.canWrite() Parameters: This
2 min read
File isHidden() method in Java with Examples
The isHidden() function is a part of File class in Java . This function determines whether the is a file or Directory denoted by the abstract filename is Hidden or not.The function returns true if the abstract file path is Hidden else return false. Function signature: public boolean isHidden() Syntax: file.isHidden() Parameters: This method does no
2 min read
File lastModified() method in Java with Examples
The lastModified() function is a part of File class in Java . This function returns the time denoted by the this abstract pathname was last modified.The function returns long value measured in milliseconds, representing the time the file was last modified else returns 0L if the file does not exists or if an exception occurs. Function signature: pub
2 min read
File createNewFile() method in Java with Examples
The createNewFile() function is a part of File class in Java . This function creates new empty file. The function returns true if the abstract file path does not exist and a new file is created. It returns false if the filename already exists. Function signature: public boolean createNewFile() Syntax: boolean var = file.createNewFile(); Parameters:
2 min read
File exists() method in Java with examples
The exists() function is a part of the File class in Java. This function determines whether the is a file or directory denoted by the abstract filename exists or not. The function returns true if the abstract file path exists or else returns false. Syntax: public boolean exists()file.exists() Parameters: This method does not accept any parameter. R
2 min read