Created
April 10, 2017 02:44
-
-
Save hu2di/5c6f3a376dd497270f938545195946ad to your computer and use it in GitHub Desktop.
Android: Saving Object on External Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<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