我想序列化和反序列化ArrayList <employee>但我一直收到警告?</employee>

时间:2014-05-09 16:19:27

标签: java file serialization io

我想将Array Employees序列化和反序列化到文件中。

我在编译和运行时不断收到类型安全警告。

有没有比我这样做更好的方法。我希望能够将条目ArrayList写入序列化文件,然后将其发送给某人并让他们能够对其进行反序列化。

问题:如何摆脱未经检查类型安全的代码?

警告:

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

代码:

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);
   }
}

1 个答案:

答案 0 :(得分:17)

您唯一的选择是使用@SuppressWarnings("unchecked")注释。