Question

I want to serialize and de-serialize an Array of Employees into and out of a file.

I keep getting a Type Safety warning on my compile and run.

Is there a better way to do this than the way that I did it. I want to be able to write the entry ArrayList to the serialized file and then to send that to someone and have them be able to de-serialize it.

Question: How can I rid my code of the unchecked type safety?

Warning:

Type safety: Unchecked cast from Object to ArrayList<Employee>

Code:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class TestSer implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    ArrayList<Employee> eee = new ArrayList<Employee>();
    ArrayList<Employee> newEmp = new ArrayList<Employee>();

    private void readSerFile() {

          try
          {
             FileInputStream fileIn = new FileInputStream("C:\\Users\\itpr13266\\Desktop\\test.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn);
             newEmp = (ArrayList<Employee>) in.readObject();
             in.close();
             fileIn.close();
          } catch(IOException i) {
             i.printStackTrace();
             return;
          }catch(ClassNotFoundException c) {
             System.out.println("Employee class not found");
             c.printStackTrace();
             return;
          }

          for (Employee ee : eee) { 
              System.out.println("Deserialized Employee...");
              System.out.println("Name:                   " + ee.name);
              System.out.println("Address:                " + ee.address);
              System.out.println("SSN:                    " + ee.SSN);
              System.out.println("Number:                 " + ee.number);   
          }
    }

    private void writeSer() {
        for (int i=0; i < 10; i++) {
            eee.add(new Employee("Name" + Integer.toString(i), "Test Address", 12345678));
        }

        try
        {
           FileOutputStream fileOut = new FileOutputStream("C:\\Users\\itpr13266\\Desktop\\test.ser");
           ObjectOutputStream out   = new ObjectOutputStream(fileOut);
           out.writeObject(eee);
           out.close();
           fileOut.close();
           System.out.printf("Serialized data is saved in /tmp/employee.ser");
        } catch(IOException i) {
            i.printStackTrace();
        }   
    }

    public static void main(String [] args) {
        TestSer tempObject = new TestSer();
        tempObject.writeSer();
        System.out.println("---");
        tempObject.readSerFile();
    }

}

class Employee implements java.io.Serializable
{
    Employee(String n, String a, int number) {
        this.name = n;
        this.address = a;
        this.number = number;
    }

   private static final long serialVersionUID = 1L;
   public String name = "";
   public String address = "";
   public transient int SSN = 0;
   public int number = 0;

   public void mailCheck() {
      System.out.println("Mailing a check to " + name + " " + address);
   }
}
Was it helpful?

Solution

Your only option is to use the @SuppressWarnings("unchecked") annotation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top