How to read a serialized object from a file in the raw folder in android?

By | July 30, 2012

In my previous post I showed how to serialize an object and save it in sdcard in a file and retrieve it.

Pull out the file from the sdcard and then create a folder named “raw” inside the “res” folder and copy it.

Serialization pull out file.
This example shows how to retreive it when it is in the res/raw folder.

Here is the code that does this.

 public void readFromResources(){
    	 try
         {
         	InputStream is = getResources().openRawResource(R.raw.save_object);
         	ObjectInputStream ois = new ObjectInputStream(is);
         	Person person = (Person)ois.readObject();
         	Log.v("Person : >> Name : ",person.getName() + ", Address : " + person.getAddress() + ", Number : " + person.getNumber());
         }
         catch(Exception ex)
         {
        	 Log.v("Serialization Error >> ",ex.getMessage());
         	 ex.printStackTrace();
         }
    }

You are done. Go on and run it.
Check the Logcat for the results.

Leave a Reply

Your email address will not be published. Required fields are marked *