2017-05-18 4 views
0

객체를 직렬화하는 방법을 살펴 보았 기 때문에 네트워킹을 통해 쉽고 빠르게 객체를 보낼 수 있습니다. 이 경우 하나의 커다란 문제가 있습니다. 배열에서 값을 초기화 한 후에는 배열의 직렬화 된 Object가 항상 초기화 된 숫자로 유지됩니다. 나는 이것이 확실한 지 확신하지 못한다. 왜냐하면 int가 디폴트 값 0을 가지고 있다고 확신하기 때문에, 내가 그것을 변경하는 대신에 0으로 머물러 있지 않을까?int 배열에서 값을 초기화 한 후 배열에 대한 업데이트가 직렬에 존재하지 않습니다.

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { 
    FileOutputStream ostr = new FileOutputStream(new File("ObjectFile.yeh")); 
    FileInputStream istr = new FileInputStream(new File("ObjectFile.yeh")); 
    ObjectOutputStream out= new ObjectOutputStream(ostr); 
    int[] hi = new int[5]; 
    hi[0]=3; 
    out.writeUTF("This is an Array"); 
    out.writeObject(hi); 
    hi[0]=5; 
    out.writeObject(hi); 
    out.writeUTF("Array Over"); 
    out.close(); 
    ObjectInputStream in = new ObjectInputStream(istr); 
    System.out.println(in.readUTF()); 
    int[] h = (int[]) in.readObject(); 
    System.out.println(h[0]); 
    int[] j = (int[]) in.readObject(); 
    System.out.println(j[0]); 
    System.out.println(in.readUTF()); 
    in.close(); 
} 

나는이 문제를 해결하는 방법 다음 코드는

This is an Array 
3 
3 
Array Over 

을 인쇄 실행하면?

업데이트 : 개체가 한 번 직렬화 된 후,이 시리얼 변경하지 않는 것입니다 발견

더 실험 후. 다음과 같이 내가 문자열 배열을 편집하고 그것의 내용이 개체 스트림

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { 
    FileOutputStream ostr = new FileOutputStream(new File("ObjectFile.yeh")); 
    FileInputStream istr = new FileInputStream(new File("ObjectFile.yeh")); 
    ObjectOutputStream out= new ObjectOutputStream(ostr); 
    String[] hi = new String[5]; 
    hi[0]="Hi"; 
    out.writeUTF("This is an Array"); 
    out.writeObject(hi); 
    hi[0]="Hello"; 
    hi[1]="Hoopla"; 
    out.writeObject(hi); 
    out.writeUTF("Array Over"); 
    out.close(); 
    ObjectInputStream in = new ObjectInputStream(istr); 
    System.out.println(in.readUTF()); 
    String[] h = (String[]) in.readObject(); 
    System.out.println(h[0]+" "+h[1]); 
    String[] j = (String[]) in.readObject(); 
    System.out.println(j[0]+" "+j[1]); 
    System.out.println(in.readUTF()); 
    in.close(); 
} 

를 통해 전송 된 후 변경하지 마십시오 그리고 이것은

This is an Array 
Hi null 
Hi null 
Array Over 

답변

1

이 그것을 변경하기 전에 출력 스트림을 다시 인쇄합니다.

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { 
    FileOutputStream ostr = new FileOutputStream(new File("ObjectFile.yeh")); 
    FileInputStream istr = new FileInputStream(new File("ObjectFile.yeh")); 
    ObjectOutputStream out= new ObjectOutputStream(ostr); 
    int[] hi = new int[5]; 
    hi[0]=3; 
    out.writeUTF("This is an Array"); 
    out.writeObject(hi); 
    out.reset(); // Add this line 
    hi[0]=5; 
    out.writeObject(hi); 
    out.writeUTF("Array Over"); 
    out.close(); 
    ObjectInputStream in = new ObjectInputStream(istr); 
    System.out.println(in.readUTF()); 
    int[] h = (int[]) in.readObject(); 
    System.out.println(h[0]); 
    int[] j = (int[]) in.readObject(); 
    System.out.println(j[0]); 
    System.out.println(in.readUTF()); 
    in.close(); 
} 
1

이것은 의도 한 동작입니다. ObjectOutputStream는, ObjectInputStream의 구축시에 실제로 공급 된 원의 오브젝트를 실제로 인도하지 않습니다. 그들을 인쇄하면 두 개의 메모리 위치가 서로 다른 것을 알 수 있습니다.

int[] arr = new int[5]; 
System.out.println(arr); 
arr[0]=3; 
out.writeObject(arr); 
arr[0]=5; 
out.writeObject(arr); 
out.close(); 

ObjectInputStream in = new ObjectInputStream(istr); 
System.out.println(in.readObject()); 
System.out.println(in.readObject()); 

// Prints 
// [[email protected] 
// [[email protected] 
// [[email protected] 

동일한 값을 가진 새 개체를 만들고 두 개의 메모리 위치를 매핑하여 개체를 "복제"합니다. 두 번째로 writeObject()를 호출하면 기존 객체를 이미 찾았 기 때문에 새 객체를 만들지 않습니다.

해결 방법 : Sending the same but modifed object over ObjectOutputStream

+0

'도 클론 개체, 나 같은 값을 갖는 새 개체를 만들고, 나 두 개의 메모리 위치를 매핑 ObjectOutputStream'. 송신자와 수신자가 '메모리 위치'가 다르다는 사실은 부적합합니다. 유일한 관련 사실은 'ObjectOutputStream'은 마지막 두 개의'메모리 주소 '에서 볼 수있는 것처럼 동일한 객체에 대한 참조를 보존한다는 것입니다. 그리고 당신은 해결책을 제공하지 않았습니다. – EJP