Monday, June 30, 2014

Serialization part 2

import java.io.*;
 
public class PlayerData implements Serializable
{
    public PlayerData(String name, int score, String username,
          String password)
    {
       this.name = name;
       this.score = score;
       this.username = username;
       this.password = password;
    }
   
    public String name;
    public int score;
    public String username;
    public String password;
}


import java.io.*;

import org.junit.Test;
   
public class SerializationExample
{
    public SerializationExample()
    {    
       // Create an instance of our PlayerData class...
       PlayerData playerData = new PlayerData("abishkar", 400, 
             "abishkar", "abishkar");
    
       // Create out file object...
       File theFile = new File("output.txt");
       
       // Create the file output stream...
       FileOutputStream fileOutputStream = null;
       try
       {
           fileOutputStream = new FileOutputStream(theFile);
       }    
       catch(FileNotFoundException e)
       {
          System.out.println(e);
       }
       
       // Create the object output stream...
       ObjectOutputStream objectOutputStream = null;
       
       try
       {
          objectOutputStream = new ObjectOutputStream
                (fileOutputStream);
       
          // Write the object to the object output stream...
          objectOutputStream.writeObject(playerData);
       }
       catch(IOException e)
       {
          System.out.println(e);
       }
       
       // Read the object back into a new instance of our
       // 'PlayerData' class
       PlayerData newPlayerData = null;
       
       // Create the file input stream...
       FileInputStream fileInputStream = null;
       try
       {
           fileInputStream = new FileInputStream(theFile);
       }
       catch(FileNotFoundException e)
       {
          System.out.println(e);
       }
       
       // Create the object input stream...
       ObjectInputStream objectInputStream = null;
       
       try
       {
          objectInputStream = new ObjectInputStream(fileInputStream);
       
          // Read the object from the object input stream...
          newPlayerData = (PlayerData) objectInputStream
                 .readObject();
       }
       catch(ClassNotFoundException e)
       {
          System.out.println(e);
       }
       catch(IOException e)
       {
          System.out.println(e);
       }
    
       // Print what was read in...
       System.out.println("The player's name was: "+newPlayerData.name);
               
       System.out.println("The player's score was:   "+newPlayerData.score);
             
       System.out.println("The player's username was: "+newPlayerData.username);
               
       System.out.println("The player's password was: "+newPlayerData.password);
               
    }
    
    @Test
 public void    M1(){
    
       SerializationExample App = new SerializationExample();
    }
    
}
 

No comments:

Post a Comment