- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether the file can be read in Java
The method java.io.File.canRead() is used to check whether the file can be read in Java. This method returns true if the file specified by the abstract path name can be read by an application and false otherwise.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("The file can be read? " + file.canRead()); } catch(Exception e) { e.printStackTrace(); } } }
The output of the above program is as follows −
Output
The file can be read? true
Now let us understand the above program.
The method java.io.File.canRead() is used to check whether the file can be read and the boolean value that is returned by the method is printed. A code snippet that demonstrates this is given as follows −
try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("The file can be read? " + file.canRead()); } catch(Exception e) { e.printStackTrace(); }
- Related Articles
- Check whether we can write to a file in Java
- Check whether the given file is an existing file in Java
- Check whether the file is hidden or not in Java
- How can we read a JSON file in Java?
- Check whether a file is a directory in Java
- Java Program to check whether a file exists or not
- How can we read & write a file using Gson streaming API in Java?
- Can we use readUTF() to read a string from a .txt file in Java?
- Mark file or directory Read Only in Java
- Check whether the number can be made perfect square after adding 1 in Python
- How to read the data from a file in Java?
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- How can we check if file exists anywhere on the system in Java?
- If 90 pages can be read in 1 hour. How many pages can be read in one minute?
- C++ Program to Check Whether Topological Sorting can be Performed in a Graph

Advertisements