Home » Java programming language

Java File Class boolean isFile() method with Example

Java File Class boolean isFile() method: Here, we are going to learn about the boolean isFile() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 08, 2019

File Class boolean isFile()

  • This method is available in package java.io.File.isFile().
  • This method is used to check whether the file is specified by filepath is a file or not.
  • The return type of this method is Boolean i.e. The value of this method is true or false if it returns true that means the file is represented by filepath is a file else return false so it is not a file.
  • This method may raise an exception(i.e. Security Exception) if the write access is not given to the file.

Syntax:

    boolean isFile(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is Boolean i.e. it returns true then in that case file is specified by abstract file path is a file else returns false so the file is specified is not a file.

Java program to demonstrate example of isFile() method

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise an 
// exception when working with files
import java.lang.Exception;

public class ToCheckFile {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes 
            // to escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\JavaArticles");

            // By using isFile() is used to check whether the filepath 
            // is a file or not. It returns true because given filepath is a file.
            if (file1.isFile())
                System.out.println("This filepath " + " " + file1.getAbsolutePath() + " " + "is a file");
            else
                System.out.println("This filepath " + " " + file1.getAbsolutePath() + " " + "is not a file");

            // By using isFile() is used to check whether the filepath 
            // is a file or not. It returns false because given filepath 
            // is not a file .
            if (file2.isFile())
                System.out.println("This filepath " + " " + file2.getAbsolutePath() + " " + "is a file");
            else
                System.out.println("This filepath " + " " + file2.getAbsolutePath() + " " + "is not a file");

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac ToCheckFile.java

D:\Programs>java ToCheckFile
This filepath  C:\Users\computer clinic\OneDrive\Articles\myjava.txt is a file
This filepath  C:\Users\computer clinic\OneDrive\JavaArticles is not a file


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.