Determine if a file can be read


/*
  Determine if a file can be read
  This Java example shows how to determine if a particular file has a read permission
  using canRead method of Java File class.
*/

import java.io.*;

public class DetermineReadFile {

 
public static void main(String[] args) {
   
   
//create file path
   
String filePath = "C:/FileIO/ReadText.txt";
   
   
//create File object
   
File file = new File(filePath);
   
   
/*
     * To determine whether a particular file can be read use,
     * boolean canRead() method of Java File class.
     *
     * This method returns true IF AND ONLY IF the file exists and it
     * can be read (file has a read permission).
     */
    
   
if(file.canRead())
    {
     
System.out.println("File " + file.getPath() +" can be read");
   
}
   
else
   
{
     
System.out.println("File " + file.getPath() +" can not be read");
   
}
  }
}

/*
Output would be
File C:\FileIO\ReadText.txt can be read
*/