Home » Java programming language

Java File Class boolean canRead() method with Example

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

File Class boolean canRead()

  • This method is available in package java.io.File.canRead().
  • This method is used to read the file and the file is represented by the abstract filepath.
  • The return type of this method is Boolean i.e. it returns true or false if true that means file can be read which is represented by the filepath or in other words file already exists to read and returns false that means the file does not exist or not allowed to read.
  • This method may raise an exception( i.e. Security Exception) if the read access is not given to the file.

Syntax:

    boolean canRead(){
    }

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 if the file already exists and allowed to read the file which is denoted by the abstract file path returns false otherwise.

Java program to demonstrate example of canRead() 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 ReadFile {
    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\\Articles\\java.txt");

            // By using canRead() is allowed to read the file 
            // if file is already exists and it returns true 
            // if file is readable else false.
            if (file1.canRead())
                System.out.println("This file " + file1.getName() + " " + "is readable");
            else
                System.out.println("This file " + file1.getName() + " " + "is not readable");

            // By using canRead() is not allowed to read the file 
            // because this file is not already exists and it returns false.
            if (file2.canRead())
                System.out.println("This file " + file2.getName() + " " + "is readable");
            else
                System.out.println("This file " + file2.getName() + " " + "is not readable");
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac ReadFile.java
D:\Programs>java ReadFile

This file C:\Users\computer clinic\OneDrive\Articles\myjava.txt is not readable
This file C:\Users\computer clinic\OneDrive\Articles\java.txt is not readable



Comments and Discussions!

Load comments ↻






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