Reading a cache file previously written by the same application

I am writing to a file using the following code:

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 - there are no errors, and the file ends up with what I expect from it. However, when I read it through openFileInput (), I get an exception saying that path separators are not allowed

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

Fairly honest, containing a slash, but how else can I specify the path to the file I want to open? Of course, there must be a way to do this, but I can’t find the answers through Google (β€œread”, β€œcache” and β€œfile” are not the most niche conditions ...), so I thought that I would try a human look. Thanks in advance!

+3
source share
1 answer

, :

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

Source: https://habr.com/ru/post/1790705/


All Articles