尝试检查内部存储中是否存在文件

2022-09-01 04:43:04

下面的代码是我尝试识别内部存储中是否存在文件的方式。MODE_PRIVATE

public boolean isset(String filename){
    FileInputStream fos = null;
    try {
       fos = openFileInput(filename);
       //fos = openFileInput(getFilesDir()+"/"+filename);
       if (fos != null) {
         return true;
       }else{
         return false;
       }
    } catch (FileNotFoundException e) {
        return false;
    }

    //File file=new File(mContext.getFilesDir(),filename);

    //boolean exists = fos.exists();
    }

但是,它会进入异常,并且不会继续执行代码。它不会做回报。为什么?


答案 1

希望这种方法对您有所帮助。

public boolean fileExist(String fname){
    File file = getBaseContext().getFileStreamPath(fname);
    return file.exists();
}

答案 2

对于内部存储,这适用于我:

public boolean isFilePresent(String fileName) {
    String path = getContext().getFilesDir().getAbsolutePath() + "/" + fileName;
    File file = new File(path);
    return file.exists();
}

推荐