Skip to content

Instantly share code, notes, and snippets.

@hu2di
Created April 10, 2017 02:44
Show Gist options
  • Save hu2di/5c6f3a376dd497270f938545195946ad to your computer and use it in GitHub Desktop.
Save hu2di/5c6f3a376dd497270f938545195946ad to your computer and use it in GitHub Desktop.
Android: Saving Object on External Storage
//<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public void ReadFile() {
try {
File f = new File(filename);
FileInputStream fIn = context.openFileInput(f.getPath());
ObjectInputStream oIn = new ObjectInputStream(fIn);
myPost = (ArrayList<Status>) oIn.readObject();
oIn.close();
fIn.close();
Log.d("myLog", "Readed");
} catch (FileNotFoundException e) {
WriteFile();
} catch (Exception e) {
Log.d("myLog", "Error Read File: " + e.toString());
}
}
public void WriteFile() {
try {
File f = new File(context.getFilesDir(), filename);
f.createNewFile();
FileOutputStream fOut = context.openFileOutput(filename, Context.MODE_PRIVATE);
ObjectOutputStream oOut = new ObjectOutputStream(fOut);
oOut.writeObject(myPost);
oOut.close();
fOut.close();
Log.d("myLog", "Writed");
} catch (Exception e) {
Log.d("myLog", "Error Write File" + e.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment