Java 文本冒险游戏保存到文件,但当我试图加载它停止程序

Java 文本冒险游戏保存到文件,但当我试图加载它停止程序,java,Java,我终于让我的保存功能为我的文本冒险工作了。当我尝试加载游戏时,它会说 Loading. . . 就像我做的那样,然后比赛就结束了。我不知道现在怎么把它装回房间。这是代码 保存功能 public void save(Player p, Room r){ String username = p.getUserName(); int currentRoomNumber = r.getCurrentRoomNumber(); String curren

我终于让我的保存功能为我的文本冒险工作了。当我尝试加载游戏时,它会说

Loading. . .
就像我做的那样,然后比赛就结束了。我不知道现在怎么把它装回房间。这是代码

保存功能

public void save(Player p, Room r){

        String username = p.getUserName();
        int currentRoomNumber = r.getCurrentRoomNumber();
        String currentRoomDescription = r.getCurrentRoomDescription();
        ArrayList<String> currentRoomItems = new ArrayList<>();
        ArrayList<String> currentRoomExits = new ArrayList<>();
        currentRoomItems = r.getCurrentRoomItems();
        currentRoomExits = r.getCurrentRoomExits();

        File f = new File(username + ".txt");

        if(f.exists()){

            try{
                FileOutputStream fout = new FileOutputStream(f);
                ObjectOutputStream oos = new ObjectOutputStream(fout);

                oos.writeObject(username);
                oos.writeObject(currentRoomNumber);
                oos.writeObject(currentRoomDescription);
                oos.writeObject(currentRoomItems);
                oos.writeObject(currentRoomExits);
                oos.flush();

            }catch(FileNotFoundException fnfe){

                System.out.println("File not found: " + fnfe.getLocalizedMessage());

            }catch(IOException e){

                System.out.println("IOException: " + e.getLocalizedMessage());
            }
        }
    }
负荷函数

public void load(Player p, Room r, World w){

        Sleep s = new Sleep();
        long time = 3000;

        Scanner i = new Scanner(System.in);
        System.out.print("What is your username?: ");
        String username = i.next();
        String name;
        int currentRoomNumber;
        String currentRoomDescription;
        ArrayList<String> currentRoomItems = new ArrayList<>();
        ArrayList<String> currentRoomExits = new ArrayList<>();
        File f = new File(username + ".txt");
        if(f.exists()){

            System.out.println("Loading game . . .");

            try{    

                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
                name = (String)ois.readObject();
                currentRoomNumber = (int)ois.readObject();
                currentRoomDescription = (String)ois.readObject();
                currentRoomItems = (ArrayList<String>)ois.readObject();
                currentRoomExits = (ArrayList<String>)ois.readObject();
                ois.close();

                Player.setUserName(name);
                w.CurrentRoom(currentRoomNumber, currentRoomDescription, currentRoomItems, currentRoomExits);
            }catch(Exception e){

                e.printStackTrace();
            }
        }
    }
}
我的世界级的CurrentRoom方法

void CurrentRoom(int roomNumber, String roomDescription, ArrayList<String> roomItems, ArrayList<String> roomExits){

        Room CurrentRoom = new Room();
        CurrentRoom.setCurrentRoomNumber(roomNumber);
        CurrentRoom.setCurrentRoomDescription(roomDescription);
        ArrayList<String> CurrentRoomItems = new ArrayList<String>();
        CurrentRoom.setCurrentRoomItems(roomItems);
        ArrayList<String> CurrentRoomExits = new ArrayList<String>();
        CurrentRoom.setCurrentRoomExits(roomExits);
    }
客房类

public static class Room{
    public static int currentRoomNumber;
    public static int roomNumber;
    public static String currentRoomDescription;
    public static String roomDescription;
    public static ArrayList<String> currentRoomItems;
    public static ArrayList<String> items;
    public static ArrayList<String> currentRoomExits;
    public static ArrayList<String> exits;
    public static ArrayList<String> commands;

    public int getRoomNumber(){

        return roomNumber;
    }
    public static void setRoomNumber(int roomNumber){

        Room.roomNumber = roomNumber;
    }
    public int getCurrentRoomNumber(){

        return roomNumber;
    }
    public static void setCurrentRoomNumber(int currentRoomNumber){

        Room.currentRoomNumber = roomNumber;
    }
    public String getRoomDescription(){

        return roomDescription;
    }
    public static void setRoomDescription(String roomDescription){

        Room.roomDescription = roomDescription;
    }
    public String getCurrentRoomDescription(){

        return roomDescription;
    }
    public static void setCurrentRoomDescription(String currentRoomDescription){

        Room.currentRoomDescription = roomDescription;
    }
    public ArrayList<String> getItems(){

        return items;
    }
    public static void setItems(ArrayList<String> items){

        Room.items = items;
    }
    public ArrayList<String> getCurrentRoomItems(){

        return items;
    }
    public static void setCurrentRoomItems(ArrayList<String> currentRoomItems){

        Room.currentRoomItems = items;
    }
    public ArrayList<String> getExits(){

        return exits;
    }
    public static void setExits(ArrayList<String> exits){

        Room.exits = exits;
    }
    public ArrayList<String> getCurrentRoomExits(){

        return exits;
    }
    public static void setCurrentRoomExits(ArrayList<String> currentRoomExits){

        Room.currentRoomExits = exits;
    }
    public ArrayList<String> getCOmmands(){

        return commands;
    }
    public static void setCOmmands(ArrayList<String> commands){

        Room.commands = commands;
    }
    void printItems(ArrayList<String> roomItems) {

       if (roomItems.size() > 0) {

            System.out.print("You see a ");
            if (roomItems.size() == 1) {

                System.out.println(roomItems.get(0) + ".");
            }
            if (roomItems.size() == 2) {

                System.out.println(roomItems.get(0) + " and a " + roomItems.get(1) + ".");
            }
            if (roomItems.size() == 3) {

                System.out.println(roomItems.get(0) + ", " + roomItems.get(1) + " and a " + roomItems.get(2) + ".");
            }
        }
    }
    void printExits(ArrayList<String> roomExits){

        if(roomExits.size() > 0){

            System.out.print("It looks like you can go ");
            if(roomExits.size() == 1){

                System.out.println(roomExits.get(0) + ".");
            }
             if (roomExits.size() == 2) {

                System.out.println(roomExits.get(0) + " or " + roomExits.get(1) + ".");
            }
            if (roomExits.size() == 3) {

                System.out.println(roomExits.get(0) + ", " + roomExits.get(1) + ", or " + roomExits.get(2) + ".");
            }
            if(roomExits.size() == 4){

                System.out.println(roomExits.get(0) + ", " + roomExits.get(1) + ", " + roomExits.get(2) + ", or " + roomExits.get(3) + ".");
            }
        }
    }
}

我相信这就是所有相关的代码。我很抱歉,如果我把不相关的代码,我会删除任何不需要的。我找到了如何编码来保存和加载这个网站,但我不知道如何让它真正加载我的游戏。非常感谢您的帮助。

您似乎没有在写入文件后关闭文件。尝试在oos之后添加oos.close,或将其替换为oos.flushI。我不确定是否可以将多个对象保存到一个文件中。无论哪种方式,都可以使用一个从文件中写入/读取的可序列化对象SavedGame。随机提示:静态保存调用Player.setUserName,但加载调用p.getUserName。应该使用其中一种。谢谢你的评论。玩过之后,我让它正常工作了。