1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* 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:FileIOReadText.txt can be read */ |
Add Comment