Wednesday, July 25, 2012

Android - file I/O

Here is my code reading/writing some simple JSON. Hope it helps you out!
private void writeToFile(String jsonString) {
Log.d(TAG, "Writing JSON to file: " + jsonString);
try {
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);


} catch (FileNotFoundException e) {
throw new WorldClockException(e);
} catch (IOException e) {
throw new WorldClockException(e);

Open the file in private mode - only owning application may read/write to such a file. Write the string, flush and close.

The close() should ideally be in a finally block but I'm still trying to figure out Android so this is not perfect!

Also posted on

No comments: