序列化。无法加载对象

问题描述 投票:-3回答:1

我想加载我的类Floyd并使用它的方法FW,但是Intellij write无法解析符号a。我在Floyd类中写了“import.io *”和“implements Serializable”

    import java.io.*;

public class jj {
    public void smth() {

        Floyd fw = new Floyd();
        try {
            FileOutputStream fs = new FileOutputStream("Floyd.ser");
            ObjectOutputStream os = new ObjectOutputStream(fs);
            os.writeObject(fw);
            os.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }


        try{

            ObjectInputStream osNew = new ObjectInputStream(new FileInputStream("Floyd.ser"));
            Floyd a = (Floyd) osNew.readObject();
            osNew.close();
        } catch (Exception ex1){
            ex1.printStackTrace();
        }

        a.FW();
    }
}
java serialization
1个回答
1
投票

在第二个try语句中移动a.FW()

try
{
     ObjectInputStream osNew = new ObjectInputStream(new FileInputStream("Floyd.ser"));
     Floyd a = (Floyd) osNew.readObject();
     a.FW()
     osNew.close();
} catch (Exception ex1){
     ex1.printStackTrace();
}

你已经在try块中初始化了变量,因此osNew的可见性不会超出try块,这样IDE就可以了解cannot resolve symbol a

© www.soinside.com 2019 - 2024. All rights reserved.