2017-12-12 12 views
-2

다음 개체를 만들어 직렬화하고 파일에 쓰려고하지만 시도한 것과 관계없이 빈 개체가 항상 파일에 쓰여집니다.파일에 개체를 serialize하면 파일에 빈 개체가 생성됩니다.

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.EOFException; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 

public class FileIO implements Serializable { 

    private static final long serialVersionUID = 1L; 
    private VIAModel viaModel1; 
    private VIAView viaView1 = new VIAView(); 
    private VIAController viaContr = new VIAController(); 

    public void setVIAModelFromFile() throws IOException, ClassNotFoundException, EOFException { 
     boolean endOfFile = false; 
     FileInputStream fstream = new FileInputStream("viaModel.ser"); 
     ObjectInputStream inputFile = new ObjectInputStream(fstream); 

     while (!endOfFile) { 
      try { 
       viaModel1 = (VIAModel) inputFile.readObject(); 
      } catch (EOFException eof) { 
       endOfFile = true; 
      } 
     } 
     inputFile.close(); 
    } 

    public void setToFile() throws IOException { 
     viaContr = viaView1.getController(); 
     viaModel1.setEventList(viaContr.getVIAMod().getEventList()); 
     System.out.println(viaModel1.getEventList().getListOfEvents()); 
     FileOutputStream fstream = new FileOutputStream("viaModel.ser"); 
     ObjectOutputStream outputFile = new ObjectOutputStream(fstream); 

     try { 
      outputFile.writeObject(viaModel1); 
      outputFile.close(); 
     } catch (FileNotFoundException e) { 
      System.out.println("File not found."); 
     } catch (IOException ioe) { 
      System.out.println("Error."); 
      ioe.printStackTrace(); 
     } 
    } 

    public VIAModel getVIAModel() { 
     return viaModel1; 
    } 

    public void setVIAModel(VIAModel viamod) { 
     this.viaModel1 = viamod; 
    } 

} 

기록되는 객체는 내부의 모든 객체에서 직렬화 가능하며 직렬화 할 수없는 객체는 수동으로 직렬화됩니다. system.out.print는 프로그램에 입력 된 정보로 개체를 표시하지만이 정보는 .ser 파일에 전혀 나타나지 않으므로 나중에 빈 개체 만 읽을 수 있습니다. 당신의 아래 코드는 마지막 개체가 당신이 입력 파일에서 정확한 내용을 읽고 있는지 확인 읽기 끝날에서

import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
import javafx.beans.property.SimpleStringProperty; 

public class Events implements Serializable { 
/** 
* 
*/ 
private static final long serialVersionUID = 5596571541918537611L; 
private transient SimpleStringProperty name = new SimpleStringProperty(""); 
private transient SimpleStringProperty date = new SimpleStringProperty(""); 
private transient SimpleStringProperty duration = new SimpleStringProperty(""); 
private transient SimpleStringProperty type = new SimpleStringProperty(""); 
private transient SimpleStringProperty location = new SimpleStringProperty(""); 
private transient SimpleStringProperty category = new SimpleStringProperty(""); 
// private Lecturer conductor; 
private transient SimpleStringProperty price = new SimpleStringProperty(""); 
private transient SimpleStringProperty minPartic = new SimpleStringProperty(""); 
private transient SimpleStringProperty maxPartic = new SimpleStringProperty(""); 
private boolean isFinalized = false; 
// ArrayList<Members> eventMembList = new ArrayList<>(); 

public Events(String name, String date, String duration, String type, String location, String category, 
     /* Lecturer conductor, */ String price, String minPartic, String maxPartic, boolean isFinalized) { 
    setName(name); 
    setDate(date); 
    setDuration(duration); 
    setType(type); 
    setLocation(location); 
    setCategory(category); 
    setPrice(price); 
    setMinPartic(minPartic); 
    setMaxPartic(maxPartic); 
    this.isFinalized = isFinalized; 
} 

public Events() { 
    this("","","","","","","","","",false); 
} 

public String getName() { 
    return name.get(); 
} 

public void setName(String name) { 
    this.name.set(name); 
} 

public String getDate() { 
    return date.get(); 
} 

public void setDate(String date) { 
    this.date.set(date); 
} 

public String getDuration() { 
    return duration.get(); 
} 

public void setDuration(String duration) { 
    this.duration.set(duration); 
} 

public String getType() { 
    return type.get(); 
} 

public void setType(String type) { 
    this.type.set(type); 
} 

public String getLocation() { 
    return location.get(); 
} 

public void setLocation(String location) { 
    this.location.set(location); 
} 

public String getCategory() { 
    return category.get(); 
} 

public void setCategory(String category) { 
    this.category.set(category); 
} 

public String getPrice() { 
    return price.get(); 
} 

public void setPrice(String price) { 
    this.price.set(price); 
} 

public String getMinPartic() { 
    return minPartic.get(); 
} 

public void setMinPartic(String minPartic) { 
    this.minPartic.set(minPartic); 
} 

public String getMaxPartic() { 
    return maxPartic.get(); 
} 

public void setMaxPartic(String maxPartic) { 
    this.maxPartic.set(maxPartic); 
} 

public boolean isFinalized() { 
    return isFinalized; 
} 

public void setFinalized(boolean isFinalized) { 
    this.isFinalized = isFinalized; 
} 

public void finalizeEvent() { 
    this.isFinalized = true; 
} 
// public void addMemToEvent(Members member) { 
// eventMembList.add(member); 
// } 
public String toString() { 
    return this.name + "\n" + this.date+ "\n" + this.duration+ "\n" + this.type+ "\n" + this.location+ "\n" + this.category+ "\n" + this.price+ "\n" + this.minPartic+ "\n" + this.maxPartic+ "\n" + this.isFinalized; 
} 

public void readExternal(ObjectInputStream in) throws IOException, ClassNotFoundException { 
    in.defaultReadObject(); 
    name = new SimpleStringProperty((String) in.readObject()); 
    date = new SimpleStringProperty((String) in.readObject()); 
    duration = new SimpleStringProperty((String) in.readObject()); 
    type = new SimpleStringProperty((String) in.readObject()); 
    location = new SimpleStringProperty((String) in.readObject()); 
    category = new SimpleStringProperty((String) in.readObject()); 
    price = new SimpleStringProperty((String) in.readObject()); 
    minPartic = new SimpleStringProperty((String) in.readObject()); 
    maxPartic = new SimpleStringProperty((String) in.readObject()); 
} 

public void writeExternal(ObjectOutputStream out) throws IOException { 
    out.defaultWriteObject(); 
    out.writeObject(name.get()); 
    out.writeObject(date.get()); 
    out.writeObject(duration.get()); 
    out.writeObject(type.get()); 
    out.writeObject(location.get()); 
    out.writeObject(category.get()); 
    out.writeObject(price.get()); 
    out.writeObject(minPartic.get()); 
    out.writeObject(maxPartic.get()); 

} 

}

+0

'.ser 파일에 정보가 전혀 표시되지 않음'을 정의하십시오. – EJP

+0

사과드립니다. .ser 파일은 작성된 viaModel1 개체에 있던 내용이없는 빈 개체를 표시합니다. –

+0

어떻게 표시 되나요? 그리고'ViaModel','ViaView', 그리고'VIAController'의 정의는 어디에 있습니까? – EJP

답변

0

. 새 VIAModel 개체를 채우는 다음 말했듯이 그것은 정확히 파일

while (!endOfFile) { 
     try { 
      viaModel1 = (VIAModel) inputFile.readObject(); 
     } catch (EOFException eof) { 
      endOfFile = true; 
    } 
+0

파일에 1 개의 개체 만 쓰고 파일에서 1 개의 개체를 읽는 것이 의도적으로 설정되었습니다. 다른 모든 객체는이 객체 내에 포함됩니다. 내가 한 첫 번째 작업은 파일에서 시작시 읽을 수있는 빈 개체를 만드는 것입니다. 개체는 편집 할 수 있으며 프로그램 닫기 직전에 파일에 저장됩니다. 위의 코드에서 viaModel1의 system.out.print는 추가 된 객체를 보여 주지만 어떤 이유로 인해 파일에 출력되지 않습니다. –

+0

@KevinAdams 그렇다면 왜 당신은 반복문을 읽고 있습니까? – EJP

+0

내가 추측하는 실제 이유가 없습니다. 지금 루프를 제거하겠습니다. –

0

에 쓰기 시도했다. 당신은

public class Events implements Serializable 

transient 분야의 전체 시리즈, 또한

public void readExternal(ObjectInputStream in) 

및이 방법은 호출되지 않습니다

public void writeExternal(ObjectOutputStream out) 

있습니다. 이러한 메서드 서명 중 하나에 대해서는 개체 직렬화 사양에 아무 것도 없습니다. 이 클래스는 직렬화하려면

, 당신은 하나 SimpleStringPropertySerializable 경우, 전역 transient을 제거하고이 방법을 제거하거나 extends Externalizable 확인하고 결과 컴파일 오류를 수정해야합니다.

이 될 수 없습니다. 고유 한 의미 및 서명을 작성한 다음 Java가 구현하지 않는 이유가 궁금합니다.

+0

무언가가 무식한 경우 내 apolagies. 나는 현재 공부하고 있으며 정직하게 대하면 이걸로 내 깊이를 아주 많이 벗어납니다. 내가 말한대로 변경을 구현하고 오류가 발생했습니다. guiView.VIAModel; Serializable과 Externalizable 호환되지 않음 –

+0

변경 사항을 구현 했습니까? 내가 너에게 대안을 줬어. – EJP

+0

죄송합니다. 가능한 한 가지 방법 만있었습니다. SimpleStringProperty는 직렬화 가능하지 않기 때문에 일시적 제거는 작동하지 않습니다. 두 번째 방법을 시도하고 그 오류가 발생했습니다. 그런 다음 Externalizable을 사용하지 않고도 Serializable에서 readObject 및 writeObject를 대신 사용할 수 있음을 알게되었습니다. –

1

SimpleStringProperty를 String으로 변경하면 완벽하게 작동하는 것처럼 보이며 수정하는 데 필요한 지식이없는 일련의 문제가 모두 제거됩니다.