6

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);
   }
}
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
Doug Hauf
  • 3,025
  • 8
  • 46
  • 70

1 Answers1

17

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

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • If I change the declaration to Object newEmp = new ArrayList(); then the untype check error goes away. Is that because Object can actually be anything that you want it to be? – Doug Hauf May 09 '14 at 16:23
  • And if you want to double-check the input (it's always a good idea to double-check input), you can iterate over the list and check that each of its elements is `instanceof Employee`. – yshavit May 09 '14 at 16:23
  • 1
    That's correct, and the other case is the opposite for you needed an explicit casting. – Bhesh Gurung May 09 '14 at 16:35
  • Can you put up a simple example of this. – Doug Hauf May 09 '14 at 17:51
  • But if you go from `Object` to `ArrayList`, you will actually get an `ArrayList` object, correct? I would feel better about suppressing the warning if I was certain my goal was achieved. ;) – Azurespot May 13 '16 at 00:59