The Wayback Machine - https://web.archive.org/web/20240710215809/https://www.geeksforgeeks.org/files-isreadable-method-in-java-with-examples/
Open In App

Files isReadable() method in Java with Examples

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

isReadable() method of java.nio.file.Files help us to check whether Java virtual machine has appropriate privileges that would allow it to open this file for reading or not. This method test file is readable or not. This method checks that a file exists or not and if file exists then it is readable or not. This method returns true if the file exists and is readable or it would return false if the following cases:

  • file does not exist
  • execute access would be denied because the Java virtual machine has insufficient privileges,

Syntax:

public static boolean isReadable(Path path)

Parameters: This method accepts a parameter path which is the path to the file to check. 

Return value: This method returns true if the file exists and is readable or it would return false if the following cases:

  • file does not exist
  • execute access would be denied because the Java virtual machine has insufficient privileges,

Exception: This method will throw SecurityException in the case of the default provider, and a security manager is installed, the checkRead is invoked to check read access to the file. Below programs illustrate isReadable(Path) method:

Program 1: 

Java




// Java program to demonstrate
// Files.isReadable() method
 
import java.io.IOException;
import java.nio.file.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create object of Path
        // This file is available on windows and
        // It is a readable file.
 
        Path path = Paths.get("D:\\GIT_EWS_PROJECTS\\logger"
                              + "\\src\\logger"
                              + "\\GFG.java");
 
        // check whether this file
        // is readable or not
        boolean result;
        result = Files.isReadable(path);
 
        System.out.println("File " + path
                           + " is Readable = " + result);
    }
}


Output:

Program 2: 

Java




// Java program to demonstrate
// Files.isReadable() method
 
import java.io.IOException;
import java.nio.file.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create an object of Path
        // This file is available on windows and
        // It is not a readable file.
 
        Path path = Paths.get("D:\\User Aman\\"
                              + "Documents\\MobaXterm\\"
                              + "\\ArrayList.docx");
 
        // check whether this file
        // is readable or not
        boolean result;
        result = Files.isReadable(path);
 
        System.out.println("File " + path
                           + " is Readable = " + result);
    }
}


Output:

Example:

Java




import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
 
public class GFG {
    public static void main(String[] args)
    {
        String fileName = "test1.txt";
        boolean isFileReadable
            = Files.isReadable(Paths.get(fileName));
        System.out.println("Is " + fileName + " readable? "
                           + isFileReadable);
    }
}


Output:

Is test1.txt readable? false


Similar Reads

Files deleteIfExists() method in Java with Examples
The deleteIfExists() method of java.nio.file.Files help us to delete a file if the file exists at the path. we pass the path of the file as a parameter to this method. This method will return true if the file was deleted by this method; false if the file could not be deleted because it did not exist. If the file is a symbolic link then the symbolic
3 min read
Files delete() method in Java with Examples
The delete() method of java.nio.file.Files help us to delete a file located at the path passed as a parameter. This method may not be atomic with respect to other file system operations. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted. If the file is a directory then this method will delete
3 min read
Files getFileStore() method in Java with Examples
getFileStore() method of java.nio.file.Files help us to return the FileStore object which represents the file store where a file is located. Once you get a reference to the FileStore you can apply filestore type of operation to get information of filestore. Syntax: public static FileStore getFileStore(Path path) throws IOException Parameters: This
2 min read
Files isHidden() method in Java with Examples
isHidden() method of java.nio.file.Files help us to check whether or not a file is hidden. This method return true if the file is considered hidden else it returns false. The exact definition of hidden is platform or provider dependent. On UNIX, for example, a file is considered to be hidden if its name begins with a period character ('.'). On Wind
2 min read
Files isWritable() method in Java with Examples
isWritable() method of java.nio.file.Files help us to check whether Java virtual machine has appropriate privileges that would allow it to open this file for writing or not. It means this method test file is writable or not. This method checks that a file exists or not and if file exists then it is writable or not. This method returns true if the f
2 min read
Files isExecutable() method in Java with Examples
isExecutable() method of java.nio.file.Files help us to check whether a file is executable. This method checks that a file exists on this path or not and if it exists then Java virtual machine has appropriate privileges to execute the file or not. The semantics may differ when checking access to a directory. For example, on UNIX systems, checking f
2 min read
Files size() method in Java with Examples
size() method of java.nio.file.Files help us to get the size of a file (in bytes). This method returns the file size, in bytes by taking the path of the file as a parameter. The size may differ from the actual size on the file system due to compression, support for sparse files, or other reasons. The size of files that are not regular files is impl
2 min read
mismatch() Method of java.nio.file.Files Utility Class with Examples
mismatch() method is newly added in java jdk-12 in the java.nio.file.Files class. This method is mainly used to compare each byte of data of two files from start and return the point at which the bit differs. If the bit doesn't differ at any point then it returns "-1L" indicating that files are the same and exact copies of each other. This method m
2 min read
Files copy() Method in Java with Examples
The copy() method of java.nio.file.Files Class is used to copy bytes from a file to I/O streams or from I/O streams to a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files. Methods: Based on the type of arguments passed, the Files class provides 3 types of copy() method. Using copy(I
5 min read
Files createTempDirectory() Method in Java with Examples
The createTempDirectory() method of java.nio.file.Files Class is used to create a new directory using the given prefix and attributes. The given prefix acts as the name of the formed directory, may be null. The directory is set with given attributes. Based on the type of arguments passed, the Files class provides 2 types of createTempDirectory() me
4 min read
Practice Tags :
three90RightbarBannerImg