【问题标题】:Java FileOutputStream try block is not being run. It immediately skips to the catch blockJava FileOutputStream 尝试块未运行。它立即跳到 catch 块
【发布时间】:2021-08-04 17:09:17
【问题描述】:

这是我的字段:

final class Player implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 2676858593456923114L;
    private final transient String filename = "BSR.data";
    private int hp;
    @SuppressWarnings("rawtypes")
    private ArrayList<ArrayList> inventory = new ArrayList<>(); //This will be used to store the other 
                                                                //inventories.
    private ArrayList<String> myLevelUps = new ArrayList<>(); //This will be used to store the collection 
                                                              //of level up items the player gets.
    private ArrayList<Crystal> myCrystals = new ArrayList<>(); //This will be used to store the 
                                                               //collection of crystals that the player 
                                                              //gets.
    private ArrayList<Weapon> myWeapons = new ArrayList<>(); //This will be used to store the collection 
                                                             //of weapons that the player gets.
    private int myLevel = 0;//a modifier.
    private int myGold;

这是没有做它应该做的方法:

void save(Player player) {
        
        try
        {
        // Create a file to write game system
        FileOutputStream out = new FileOutputStream (filename);

        // Code to write instance of Player will go here
        ObjectOutputStream objectOut = new ObjectOutputStream (out);

        // Write Player system to object store
        objectOut.writeObject (player);
        
        // Close object output stream
        objectOut.close();
        
        System.out.println("Your game has been saved to" + filename);
        }
        catch (Exception e)
        {
            System.err.println ("Unable to create game data");
        }
        
    }

应该发生的是使用字段中的文件名创建文件。但是,正如问题所述,整个 try 块被立即跳过。并运行 catch 块。我检查了所有字段,它们都是可序列化的,所以这不是问题。我哪里做错了?我使用教程来获取代码。

【问题讨论】:

  • 我们需要查看异常。在你的 catch 块中,e.printStackTrace() 以便你可以看到发生了什么
  • 把catch中的代码改成e.printStackTrace(),看看哪里出错了。
  • 如果方法save在类Player中,也许你应该把它设为静态方法?
  • 您确实应该打印异常堆栈跟踪以了解更多信息。但是,如果您在尝试的第一行(初始化 FileOutputStream 的位置)遇到异常,则文件“BSR.data”很可能不在 Java 进程运行的位置。
  • 你应该了解 try-with-resources。

标签: java fileoutputstream serializable


【解决方案1】:

就像 David Zimmerman 所说,将 e.printStackTrace() 放在 catch 块中以查看异常。另外我怀疑您执行错误的方法。通过您的实施,您必须执行以下操作:

Player p = new Player(...);
p.save(p);

我用那个代码做了一个测试,它工作正常。但这很难看,因为您必须将外部引用传递给对象本身。我建议您从“保存”函数中删除“播放器播放器”参数并以这种方式编写对象:

objectOut.writeObject(this);

然后你可以这样做:

Player p = new Player();
p.save();

我用这种方式测试过,效果也很好。

如果您将资源自动关闭项放在 try-with-resources 中(如 Raedwald 所说),您可以做得更好,这样您就不必担心关闭每个资源。

try (FileOutputStream out = new FileOutputStream(filename);
ObjectOutputStream objectOut = new ObjectOutputStream(out);) {

您可以删除以下行:

objectOut.close();

【讨论】:

    猜你喜欢
    • 2022-06-10
    • 2023-03-18
    • 1970-01-01
    • 2022-01-23
    • 2012-07-11
    • 2010-12-25
    • 1970-01-01
    • 2021-11-17
    • 2013-01-01
    相关资源
    最近更新 更多