You can't short this process as String
is best format to save information in file AFAIK. Also inputStreamReader
& inputStream
are not suitable object to be written. There may be other better explanatory answer :)
How to save and retrieve InputStream in text file with less overhead
-
23-06-2022 - |
Pregunta
I am retrieving/downloading JSON
feed as InputStreamReader
and I need to pass this InputStreamReader
to JsonReader
for parsing it.
JsonReader reader = new JsonReader(isr); // isr is the InputStreamReader instance
Everything is okay. But I want to cache this InputStreamReader
or this data in some other format into internal storage for later use. For caching, I am using text file to save. (AFAIK, android default cache saves data in external storage and some devices have no external storage).
For saving this file, I am using .txt
file. But I am only able to save/cache String
formatted data into file and read it as String
. This is the code what I have written so far to write & read file:
public static void writeObject(Context context, String key, Object object)
throws IOException {
FileOutputStream fos = context
.openFileOutput(key, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
fos.close();
}
public static Object readObject(Context context, String key)
throws IOException, ClassNotFoundException {
FileInputStream fis = context.openFileInput(key);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
return object;
}
But this approach is taking too much conversion:
inputStreamReader -> inputStream -> String //to save
String -> inputStream -> inputStreamReader // retrieve
How can Save/cache this data with less overhead? Perhaps inputStreamReader
needs to be converted to outputStreamReader
or something like this to be writable. But I can't figure out the associative code to do this. What should be the code? Also can I cache this data by android's proposed way for later use intead of saving it as .txt
file?
Solución