Home > DeveloperSection > Articles > Java Enumerations: Serializing enum Types

Java Enumerations: Serializing enum Types


Java Java 
Ratings:
0 Comment(s)
 160  View(s)
Rate this:

Java Enumerations: Serializing enum Types


Earlier we learned that objects of the enum type can be serialized and compared to each other. Suppose we create an enumeration for the colors used in a drawing program. Such an enumeration may look like this:

enum ColorPalette {

RED, GREEN, BLUE                 

}                                   

 

 

We can select the color green for the current drawing with the following declaration:

ColorPalette drawingColor = ColorPalette.GREEN;

We can now save the drawingColor object to a setting file by calling the writeObject method of the ObjectOutputStream class . The code might look like this:

ObjectOutputStream outStream = new ObjectOutputStream(

                                                                                             new FileOutputStream("Settings.dat"));

outStream.writeObject(drawingColor);

 

 

Later on, to read the settings from the Settings.dat file, we would use the following code:

ObjectInputStream inStream = new ObjectInputStream(

                                                                                               new FileInputStream("Settings.dat"));

 

System.out.println("Retrieved object: "

+ (ColorPalette) inStream.readObject());

 

 

This prints GREEN to the terminal. Note that the message printed to the console is the string name of the constant rather than its ordinal value. The default implementation of the toString method of the enum class does this for us.

Earlier we learned that serialization withstands arbitrary changes in the enum type. So now, let’s modify our definition of the ColorPalette enumeration as follows:

enum ColorPalette {

RED, YELLOW, GREEN, MAGENTA, BLUE, VIOLET

}

 

 

Note that we have now added a new color (yellow) before green. If we read the previous Settings.dat file, GREEN is still printed to the console, although the ordinal value of GREEN has now changed. The trivial code for testing this is provided here:

import java.io.*;

 

public class EnumSerialization {

                     public static void main(String[] args) {

                             ColorPalette drawingColor = ColorPalette.GREEN;

                             try {

                                      System.out.println("Saving color setting");

                                      ObjectOutputStream outStream = new ObjectOutputStream(

                                                          new FileOutputStream("Settings.dat"));

                                      outStream.writeObject(drawingColor);

                                      outStream.close();

                                      ObjectInputStream inStream = new ObjectInputStream(

                                                          new FileInputStream("Settings.dat"));

                                      System.out.println("Retrieved object: "

                                                          + (ColorPalette) inStream.readObject());

                                      inStream.close();

                             } catch (IOException e) {

                                      System.out.println("Error reading/writing object");

                             } catch (ClassNotFoundException cfe) {

                                      System.out.println("Class casting error");

                             }

                     }

}

 

enum ColorPalette {

                     RED, GREEN, BLUE

}

 


Don't want to miss updates? Please click the below button!

Follow MindStick