【发布时间】:2018-12-15 11:34:08
【问题描述】:
我正在尝试在我的应用程序缓存文件夹中创建子目录,但是在尝试检索文件时我什么也没得到。我在下面有一些关于如何创建子目录以及如何从中读取的代码,也许我只是做错了什么(很明显我是大声笑)或者这不可能? (尽管我没有看到任何你看不到的地方)。谢谢大家的帮助!
创建子目录
File file = new File(getApplicationContext().getCacheDir(), "SubDir");
File file2 = new File(file, each_filename);
Toast.makeText(getApplicationContext(), file2.toString(), Toast.LENGTH_SHORT).show();
stream = new FileOutputStream(file2);
stream.write(bytes);
从中读取
File file = new File(context.getCacheDir(), "SubDir");
File newFile = new File(file, filename);
Note note;
if (newFile.exists()) {
FileInputStream fis;
ObjectInputStream ois;
try {
fis = new FileInputStream(new File(file, filename));
ois = new ObjectInputStream(fis);
note = (Note) ois.readObject();
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
return note;
}
这个我也试过了,什么都没有
String file = context.getCacheDir() + File.separator + "SubDir";
【问题讨论】:
标签: java android directory subdirectory