Updation in Serialization - Illustrating JButton

Update values in Serializable Object


Do you know? We can update values after Serializing an object. Usually overriding them is possible, but we can update them too. There may be several ways, but i have a solution for this problem. You just take a look at the example, you'll probably understand, if you can't, the explanation is there to help you. Go to main theme explanation


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class SerializeJButtonClick extends JFrame implements ActionListener
{
JButton b1,b2;
int b1count,b2count;
StoreClicks s;

public SerializeJButtonClick()
{
setTitle("Serialize JButton Click");
setSize(400,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

b1=new JButton("Button 1");
b2=new JButton("Button 2");

b1.addActionListener(this);
b2.addActionListener(this);

add(b1);
add(b2);

try
{

FileInputStream fin=new FileInputStream("sc.dat");
ObjectInputStream oin=new ObjectInputStream(fin);
s=(StoreClicks)oin.readObject();
b1.setText("You clicked me "+s.b1c+" times.");
b2.setText("You clicked me "+s.b2c+" times.");

}catch(Exception e){

try
{

FileOutputStream fout=new FileOutputStream("sc.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(new StoreClicks());
}catch(Exception e1){}

}

}

public void actionPerformed(ActionEvent ae)
{
FileInputStream fin;
ObjectInputStream oin;
StoreClicks s;

try
{

fin=new FileInputStream("sc.dat");
oin=new ObjectInputStream(fin);
s=(StoreClicks)oin.readObject();
if(ae.getSource().equals(b1))
{
s.b1c++;
b1.setText("You clicked me "+s.b1c+" times.");
}

else
{
s.b2c++;
b2.setText("You clicked me "+s.b2c+" times.");
}

FileOutputStream fout=new FileOutputStream("sc.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(s);

out.close();
fout.close();

}catch(Exception e){}

}

public static void main(String args[])
{
new SerializeJButtonClick();
}

class StoreClicks implements Serializable
{
int b1c,b2c;
}

}

Explanation


javax.swing.*: JButton, JFrame used here.

java.awt.*: FlowLayout used here.

java.awt.event.*: ActionListener, ActionEvent used here.

java.io.*: Serializable, FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream used here.

All the GUI Stuff: Hope, you can understand, if you can't refer to Create Swing Buttons post.

fin=new FileInputStream("sc.dat"): Create FileInputStream object pointing sc.dat. 

oin=new ObjectInputStream(fin): Create ObjectInputStream object pointing FileInputStream

s=(StoreClicks)oin.readObject(): Read object from ch.dat and store the object (the data in it) in s. Type casting is done because readObject() method returns java.lang.Object

Exception. The main part: This is the main logic of the program, if you can observe, exception is raised when the file sc.dat is not found and also if the object is not Serialized or if the file is unable to read. The last two chances are not possible in general. But the first one, when sc.dat is not found means that there is no data will be read and stored in s. So, i've created a new FileOutputStream object pointing to sc.dat which is created when sc.dat is not found, nextly, out points to fout and then using writeObject() the data is written, as no values are specified for the data members of s the default values for int in java (i.e. 0) are set.
You'll have to note that this case (file not found) occurs for the first time or very rarely if user deletes the file, however that may not be the one that we might have to consider, because user himself want to delete it, then it means that he might not want that data.

In actionPerformed()...


fin=new FileInputStream("sc.dat"): Points to sc.dat, it is created already (i.e. sc.dat is present).

oin=new ObjectInputStream(fin): Points to fin

s=(StoreClicks)oin.readObject(): Read object from sc.dat and store the values of the data members in s.

Why all this is done?


Simple. We want to update the values, so we'll have to retrieve the values (b1c,b2c) from the file sc.dat and then add increment them upon user clicks on JButtons.

ae.getSource(): Gets the source of the event i.e. which button the user clicked (here).

s.b1c++: Increment the value upon each click.

else: Since there are only two buttons, if not b1, then b2 will be the one on which action is performed.

fout,out,writeObject(): fout points to sc.dat, out points to fout, writeObject() writes updated values of s into sc.dat

StoreClicks: Nothing, but an inner class implementing Serializable and also containing two variables b1c, b2c which are for counting clicks for b1,b2 buttons respectively. You can also write the class outside.

Also see Understanding Java Serialization if you don't have touch with it.

No comments: