How to determine if a file can be read in Java

1 Answer

0 votes
package javaapplication1;

import java.io.File;
import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            File file = new File("d://data.txt");

            if (file.canRead()) {
                System.out.println("can be read");
            } else {
                System.out.println("can not be read");
            }

        } catch (Exception e) {
            System.out.print(e);
        }
    }
}


/*
        
run:
  
can be read
    
 */

 



answered Nov 16, 2016 by avibootz
...