Home Tutorial Java Corejava How to check a file is read-able or not in java

 
 

How to check a file is read-able or not in java
Posted on: April 12, 2010 at 12:00 AM
This tutorial demonstrate that how to rectify a file whether it have read permission or not.

Description:

This example demonstrate how to check a file have read permission or not at a specified path.  The method canRead() of the File class check for its read property of a file. As in the file path we see '\' (backslash) that is used to denote file separation but here in this example File.separator is used.  

Code:

import java.io.File;

public class CheckFileCanRead {
  public static void main(String[] a) {
    File findFile = new File("C:" + File.separator + "java-tutorial"
        + File.separator + "Test" + File.separator +
        
"anyfile.txt");
    System.out.println(findFile.canRead());
  }
}

Output:

The output of this example is either true or false depends on the file have read property at the specified path that is set in the program above.

Related Tags for How to check a file is read-able or not in java:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.