File canRead() in Java

canRead(): This method is available in the java.io.File class of Java.

Syntax:

boolean java.io.File.canRead()

This method tests whether the application can read the file denoted by this abstract pathname.

Parameters: NA

Returns: true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise.

Throws:

1. SecurityException - If a security manager exists and its java.lang.SecurityManager.checkRead(java.lang.String)method denies read access to the file.

Approach

Java

import java.io.File;

public class FilecanRead {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);

        System.out.println(file.canRead());
    }
}

Output:

false


No comments:

Post a Comment