read


Code:

// Writing
<ol style="background-color: #f8f8f8; color: #222222; font-family: monospace; font-size: 10px; line-height: 12px; list-style-position: outside; margin: 0px; padding: 0px 0px 0px 4em;"><li class="li1" style="line-height: 1.3; margin: 0px; padding: 0px;"><div class="de1">FileOutputStream fos = context.openFileOutput(SAVENAME, context.MODE_PRIVATE);</div></li><li class="li1" style="line-height: 1.3; margin: 0px; padding: 0px;"><div class="de1">ObjectOutputStream oos = new ObjectOutputStream(fos);</div></li><li class="li1" style="line-height: 1.3; margin: 0px; padding: 0px;"><div class="de1">oos.writeObject(object); oos.close();</div></li></ol><div class="de1" style="background-color: #f8f8f8; color: #222222; font-family: monospace; font-size: 10px; line-height: 1.3;">
</div><div class="de1" style="background-color: #f8f8f8; color: #222222; font-family: monospace; font-size: 10px; line-height: 1.3;">// Reading it back..</div><ol style="background-color: #f8f8f8; color: #222222; font-family: monospace; font-size: 10px; line-height: 12px; list-style-position: outside; margin: 0px; padding: 0px 0px 0px 4em;"><li class="li2" style="line-height: 1.3; margin: 0px; padding: 0px;"><div class="de2">FileInputStream fis = context.openFileInput(SAVENAME);</div></li><li class="li1" style="line-height: 1.3; margin: 0px; padding: 0px;"><div class="de1">ObjectInputStream ois = new ObjectInputStream(is);</div></li><li class="li1" style="line-height: 1.3; margin: 0px; padding: 0px;"><div class="de1"></div></li><li>Flow f = (Flow) ois.readObject(); //If the object is of type Flow</li></ol><div class="de1" style="color: #222222; font-family: monospace; font-size: 10px; line-height: 1.3;">
</div>
If you write a custom class, it must implement Serializable and if you want add @SuppressWarnings("serial") to the top of the class definition.

You can write one object, an arraylist of objects, or any type with this code.

Source

Blog Logo

Daniel Gomez Rico


Published

Image

MakinGIANTS

The findings and tips records of an Android-iOS-TheWholeShabang group

Back to Overview