Home » Java programs » Java file handling programs

Java program to check whether a file can be read or not

In this java program, we are going to check whether a given file can be read or not? To check this, we are using File.canRead() method.
Submitted by IncludeHelp, on October 28, 2017

Given a file, and we have to check whether file can be read or not using Java program.

In this program, there is a file named "C.txt" which is saved in "E:\" drive in my system, so that path is "E:/C.txt".

To check that file can be read or not, there is a method canRead() of "File" class, it returns 'true' if file can be read or returns 'false' if file cannot be read.

Program to check, file can be read or not in Java

import java.io.*;
public class DetermineFileCanBeRead 
{
	public static void main(String[] args) 
	{
		String filePath = "E:/C.txt";
		File file = new File(filePath);
		if(file.canRead())
		{
			System.out.println("File " + file.getPath() +" can be read");
		}
		else
		{
			System.out.println("File " + file.getPath() +" can not be read");
		}
	}
}

Output

File E:\A.txt can be read





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.