How to check if a file is readable?

前端 未结 5 2220
滥情空心
滥情空心 2020-12-15 11:50

I\'m writing Java 6 application and I have to check if a file is readable. However, on Windows canRead() always returns true. So I see that probabl

5条回答
  •  既然无缘
    2020-12-15 12:55

    Try to use the following code

    public boolean checkFileCanRead(File file){
        try {
            FileReader fileReader = new FileReader(file.getAbsolutePath());
            fileReader.read();
            fileReader.close();
        } catch (Exception e) {
            LOGGER.debug("Exception when checking if file could be read with message:"+e.getMessage(), e);
            return false;
        }
        return true;
    }
    

提交回复
热议问题