나는 직렬화 및 역 직렬화하려는 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);
}
}
유일한 옵션은 @SuppressWarnings("unchecked")
주석 을 사용하는 것 입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다