Java 如何在序列化中获得相同的引用?
这是我使用serialisable接口的部分。当我启动程序时,它将为每个列表创建相同的对象,但引用不同。有没有办法把它们都作为一个参考Java 如何在序列化中获得相同的引用?,java,serialization,reference,Java,Serialization,Reference,这是我使用serialisable接口的部分。当我启动程序时,它将为每个列表创建相同的对象,但引用不同。有没有办法把它们都作为一个参考 private static void quitApplication(){ System.out.println("Stopping the system...\n"); ///store all the objects by serializing try { FileOutputStream fileOut2 = n
private static void quitApplication(){
System.out.println("Stopping the system...\n");
///store all the objects by serializing
try {
FileOutputStream fileOut2 = new FileOutputStream("FlightList.ser");
ObjectOutputStream out2 = new ObjectOutputStream(fileOut2);
out2.writeObject(Flight.getFlights());
out2.close();
fileOut2.close();
}catch(IOException i) {
System.out.println("FlightList.ser ERROR");
}
try {
FileOutputStream fileOut = new FileOutputStream("CityList.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(City.getCityList());
out.close();
fileOut.close();
}catch(IOException i) {
System.out.println("CityList.ser ERROR");
}
try {
FileOutputStream fileOut1 = new FileOutputStream("GrapghL.ser");
ObjectOutputStream out1 = new ObjectOutputStream(fileOut1);
out1.writeObject(Test.flightGraph);
out1.close();
fileOut1.close();
}catch(IOException i) {
System.out.println("GrapghL.ser ERROR");
}
System.out.println("Done...Thank You For using the Airlines System");
}
private static void initializeData(){
try {
FileInputStream fileIn = new FileInputStream("CityList.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
City.setCityList((MyList<City>) in.readObject());
in.close();
fileIn.close();
}catch(Exception i){
System.out.println("CityList.ser ERROR");
}
try {
FileInputStream fileIn2 = new FileInputStream("FlightList.ser");
ObjectInputStream in2 = new ObjectInputStream(fileIn2);
Flight.setFlights((MyList<Flight>) in2.readObject());
in2.close();
fileIn2.close();
}catch(Exception i){
System.out.println("FlightList.ser ERROR");
}
try {
FileInputStream fileIn1 = new FileInputStream("GrapghL.ser");
ObjectInputStream in1 = new ObjectInputStream(fileIn1);
Test.flightGraph = (DefaultDirectedWeightedGraph<City, DefaultWeightedEdge>) in1.readObject();
in1.close();
fileIn1.close();
}catch(Exception i){
System.out.println("GrapghL.ser ERROR");
}
}
private静态应用程序(){
System.out.println(“停止系统…\n”);
///通过序列化存储所有对象
试一试{
FileOutputStream fileOut2=新的FileOutputStream(“FlightList.ser”);
ObjectOutputStream out2=新的ObjectOutputStream(fileOut2);
out2.writeObject(Flight.getFlights());
out2.close();
fileOut2.close();
}捕获(IOI异常){
System.out.println(“FlightList.ser错误”);
}
试一试{
FileOutputStream fileOut=新的FileOutputStream(“CityList.ser”);
ObjectOutputStream out=新的ObjectOutputStream(fileOut);
out.writeObject(City.getCityList());
out.close();
fileOut.close();
}捕获(IOI异常){
System.out.println(“CityList.ser错误”);
}
试一试{
FileOutputStream fileOut1=新的FileOutputStream(“graphgl.ser”);
ObjectOutputStream out1=新的ObjectOutputStream(fileOut1);
out1.writeObject(Test.flightGraph);
out1.close();
fileOut1.close();
}捕获(IOI异常){
System.out.println(“graphhl.ser错误”);
}
System.out.println(“完成……感谢您使用航空公司系统”);
}
私有静态无效初始化数据(){
试一试{
FileInputStream fileIn=newfileinputstream(“CityList.ser”);
ObjectInputStream in=新的ObjectInputStream(fileIn);
.readObject()中的City.setCityList((MyList);
in.close();
fileIn.close();
}捕获(例外情况一){
System.out.println(“CityList.ser错误”);
}
试一试{
FileInputStream fileIn2=新的FileInputStream(“FlightList.ser”);
ObjectInputStream in2=新的ObjectInputStream(fileIn2);
setFlights((MyList)in2.readObject());
in2.close();
fileIn2.close();
}捕获(例外情况一){
System.out.println(“FlightList.ser错误”);
}
试一试{
FileInputStream fileIn1=新的FileInputStream(“GrapghL.ser”);
ObjectInputStream in1=新的ObjectInputStream(fileIn1);
Test.flightGraph=(DefaultDirectedWeightedGraph)在1.readObject()中;
in1.close();
fileIn1.close();
}捕获(例外情况一){
System.out.println(“graphhl.ser错误”);
}
}
这上面实际上有一个。正如EJP所提到的,您实现了readResolve()
我重写了Java 1.6+的示例类:;-)
导入java.io.ObjectStreamException;
导入java.io.Serializable;
导入java.util.HashMap;
导入java.util.Map
公共类RememberMe实现可序列化{
/**
*按id包含所有已知实例。如果您认为需要永久丢弃某个对象,则必须将其从此映射中删除。
*/
静态最终映射knownInstances=newHashMap();
/**
*此id始终只有一个对象可访问
*/
私有最终int id;
/**
*多变的领域
*/
私有字符串名称;
/**
*另一个多变的领域
*/
私有字符串位置;
/**
*私有构造函数,以便我们可以发布现有实例
*/
private RememberMe(最终int-id、最终字符串名称、最终字符串位置){
this.id=id;
this.name=名称;
这个位置=位置;
}
/**
*这是创建和访问此类对象的方式。如果id已存在,则忽略名称和位置
*/
公共静态RememberMe getRememberMe(int-id、字符串名称、字符串位置){
已同步(已知实例){
最终RememberMe oldValue=knownInstances.get(id);
if(oldValue!=null){
返回旧值;
}否则{
final RememberMe newValue=新的RememberMe(id、名称、位置);
knownInstances.put(id,newValue);
返回新值;
}
}
}
公共int getId(){
返回id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getLocation(){
返回位置;
}
公共void集合位置(字符串位置){
这个位置=位置;
}
/**
*完全创建对象后(即调用构造函数和所有对象后)调用此函数
*已创建字段引用)。现在,我们只需确定它是同类中的第一个,还是我们已经创建了
*通过该id了解对象。
*/
私有对象readReplace()抛出ObjectStreamException{
已同步(已知实例){
最终RememberMe oldValue=knownInstances.get(id);
if(oldValue!=null){
返回旧值;
}否则{
知道实例。放置(id,this);
归还这个;
}
}
}
}
这是怎么回事?
- 有一个知道类的所有实例的映射
- 如果对象是反序列化的,我们使用
将其替换为以前创建的具有相同id的任何实例readReplace()
readReplace()
更改;因此,该类在known实例内部getRememberMe
中注册,而不是在构造函数中注册
如果您想让GC回收此类的对象,您可能需要用一些弱*变量替换HashMap
或其值。您必须重写readObject/writeObject方法。让他们成为一个整体管理者。澄清一下,你想要单例值吗,比如?Mik