Reading a cache file previously written by the same app

笑着哭i 提交于 2019-12-06 02:29:56

问题


I'm writing to a file using the code below:

File file = new File(getCacheDir(), "cachefile");
FileOutputStream fos = new FileOutputStream(file);
StringBuilder cachetext = new StringBuilder();
Iterator bri = brands.iterator();
Iterator bli = brand_id.iterator();
while(bli.hasNext()) {
    cachetext.append(bli.next() + "|" + bri.next() + System.getProperty("line.separator"));
}
fos.write(cachetext.toString().getBytes());
fos.close();

This works fine - no errors and the file ends up containing what I expect it to contain. When I go to read it via openFileInput(), however, I get an exception telling me that path separators are not allowed

FileInputStream fis = openFileInput(getCacheDir() + "/cachefile");

Fair enough, that contains a slash, but how else can I specify the path of the file I want to open? There must be a way to do this, of course, but I can't find answers via Google ('read', 'cache' and 'file' not being the most niche of terms ...) so I thought I'd try the human touch. Thanks in advance!


回答1:


you do it pretty much the same way you created the output file:

FileInputStream fis = new FileInputStream(new File(getCacheDir(), "cachefile"));


来源:https://stackoverflow.com/questions/4933474/reading-a-cache-file-previously-written-by-the-same-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!