Android应用:内部存储上新创建的文件显示在文件列表中,但尝试打开文件会创建“无此文件或目录”的异常。 (Android 8.0,在多个设备上测试过。)
//create a file in internal storage
FileOutputStream output = null;
output = openFileOutput("xxxyyy.txt", Context.MODE_PRIVATE);
String str = "Just any string";
byte[] bytes = str.getBytes();
output.write(bytes, 0, bytes.length);
output.close();
//list the files
String lst[] = fileList();
for (int i = 0; i < lst.length; i++)
{ Log.v("MM" , "INTERNAL FILES: "+lst[i]); }
//system reply: INTERNAL FILES: xxxyyy.txt, ......
//check if file exists and try to open it for reading
File file = new File("xxxyyy.txt");
Log.v("XX" , "TEST: file exists? " + file.exists());
//system reply: TEST: file exists? false
//try to open it for reading
FileInputStream input = null;
try {
input = new FileInputStream("xxxyyy.txt");
} catch (IOException e) {
Log.v("XX" , "TEST: " + e.getMessage());
}
//system reply: TEST: xxxyyy.txt (No such file or directory)
已尝试过许多变化。 任何建议都非常感谢!
答案 0 :(得分:0)
如果您需要访问使用openFileOutput
创建的文件,则需要在创建File
对象时指定正确的目录,在这种情况下:
File file = new File(getFilesDir(), "xxxyyy.txt");