Get FileNotFoundException when initialising FileInputStream with File object

前端 未结 4 1547
盖世英雄少女心
盖世英雄少女心 2020-12-11 18:04

I am trying to initialise a FileInputStream object using a File object. I am getting a FileNotFound error on the line

fis = new FileInputStream(file);
         


        
4条回答
  •  生来不讨喜
    2020-12-11 18:35

    Judging by the stacktrace you pasted in your post I'd guess that you do not have the rights to read the file.

    The File class allows you to performs useful checks on a file, some of them:

    boolean canExecute();
    boolean canRead();
    boolean canWrite();
    boolean exists();
    boolean isFile();
    boolean isDirectory();
    

    For example, you could check for: exists() && isFile() && canRead() and print a better error-message depending on the reason why you cant read the file.

提交回复
热议问题