We can save the all the synchronized Objects in Android using File Connections even if the Object is a customized or user defined object.
Saving the Objects:
step 1:
Open a file using the below code.
File suspend_f = new File("/data/data/com.rama.rama/cache/check.txt");
here i had saved the file in the application space in the device which will be removed when the application is un-installed. You can save it any where. but you should remember it for further fetching of the objects.
Step 2:
Opening an outputStream for the File object and writing the object to file as shown below.
fos = new FileOutputStream(suspend_f);
oos = new ObjectOutputStream(fos);
oos.writeInt(10);
for(int i=0 ;i<10;i++)
oos.writeObject(new Lecture("Mahesh"+i, "sta", "end", "one", true));
We should read the objects in the same order that you write objects.
Reading the Objects:
step 1:
Opening the file object just as like the above step1.
Step 2:
Opening InputStream of the File Object and reading the objects in the same order that we write in the file.
fis = new FileInputStream(suspend_f);The whole code can be downloaded from this link.
ois = new ObjectInputStream(fis);
String s = "";
int j=ois.readInt();
for(int i=0;i{
_l = (Lecture)ois.readObject();
s+=_l;
}
No comments:
Post a Comment