IllegalArgumentException:file包含路径分隔符

use*_*421 6 android

我正在尝试检查应用程序内部存储中的子目录中是否存在zip:

File file = this.getApplicationContext().getFileStreamPath(this.getFilesDir().getAbsolutePath() + "/thesubdirectory/the.zip";
if(file.exists()) {
    Log.e(this.class.getName(), "file exists");
}
Run Code Online (Sandbox Code Playgroud)

这是扔了一个java.lang.IllegalArgumentException: File /data/data/my.package.name/files/thesubdirectory/the.zip contains a path separator.

为什么会这样?我认为这是你应该检查文件是否存在的方式.

Gab*_*han 8

file.exists是.但getFileStreamPath无法获取路径,需要在该目录中获取文件名.试试这个:

File file = new File(this.getFilesDir().getAbsolutePath() + "/thesubdirectory/the.zip");
Run Code Online (Sandbox Code Playgroud)