1 Replies - 676 Views - Last Post: 24 April 2012 - 08:15 PM Rate Topic: -----

#1 labrower17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-March 12

[Error/Problem] Saving/Loading Files in text-based game

Posted 24 April 2012 - 06:53 PM

Hey everyone,
I am creating a text-based game similar to "Zork" for my high school computer class. I am trying to create a save/load file so that you can track what room you are in, items obtained, what rooms you have been in, etc. The save code I have is...

[ private void SaveSlot1() {
try{
// Catch errors in I/O if necessary.
// Open a file to write to, named SavedObjects.sav.
FileOutputStream saveFile=new FileOutputStream("D:/MyDocuments/EclipsePrjects/Labyrinthian/saves/save1.sav");
// Create an ObjectOutputStream to put objects into save file.
ObjectOutputStream save = new ObjectOutputStream(saveFile);

// now we do the save
save.writeObject(homedes);
save.writeObject(town1des);
save.writeObject(tavern1des);
save.writeObject(plains1des);
save.writeObject(cave1p1des);
save.writeObject(cave1p2des);
save.writeObject(cave1p3des);
save.writeObject(plateau1des);
save.writeObject(cliffs1des);
save.writeObject(ocave1des);
save.writeObject(ocave2des);
save.writeObject(cbottom1des);
save.writeObject(forest1des);
save.writeObject(forest1p2des);
save.writeObject(forest1p3des);
save.writeObject(mountains1des);
save.writeObject(mountains1p2des);
save.writeObject(mountains1p3des);
save.writeObject(town2des);
save.writeObject(inn1des);
save.writeObject(shop1des);
save.writeObject(currentRoom);
save.writeObject(parser);

// close the file
save.close();
}
catch(Exception exc){
exc.printStackTrace(); // If there was an error, print the info.
}
}
]

And then I execute the method when the user types "save 1".

[
if (commandWord.equals("save 1"))
SaveSlot1();
]

The load code I have is...

[
private void SaveSlot3() {
try{
// Catch errors in I/O if necessary.
// Open a file to write to, named SavedObjects.sav.
FileOutputStream saveFile=new FileOutputStream("D:/MyDocuments/EclipsePrjects/Labyrinthian/saves/save3.sav");
// Create an ObjectOutputStream to put objects into save file.
ObjectOutputStream save = new ObjectOutputStream(saveFile);

// now we do the save
save.writeObject(homedes);
save.writeObject(town1des);
save.writeObject(tavern1des);
save.writeObject(plains1des);
save.writeObject(cave1p1des);
save.writeObject(cave1p2des);
save.writeObject(cave1p3des);
save.writeObject(plateau1des);
save.writeObject(cliffs1des);
save.writeObject(ocave1des);
save.writeObject(ocave2des);
save.writeObject(cbottom1des);
save.writeObject(forest1des);
save.writeObject(forest1p2des);
save.writeObject(forest1p3des);
save.writeObject(mountains1des);
save.writeObject(mountains1p2des);
save.writeObject(mountains1p3des);
save.writeObject(town2des);
save.writeObject(inn1des);
save.writeObject(shop1des);
save.writeObject(currentRoom);
save.writeObject(parser);

// close the file
save.close();
}
catch(Exception exc){
exc.printStackTrace(); // If there was an error, print the info.
}
}
]

Then I execute the method when the user types "load 1".

[
if (commandWord.equals("load 1"));
LoadSlot1();
]

Whenever I run the game I get this error every time I type a command.

[
java.io.FileNotFoundException: save1.sav (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at Game.LoadSlot1(Game.java:359)
at Game.processCommand(Game.java:188)
at Game.play(Game.java:138)
at Main.main(Main.java:20)
java.io.FileNotFoundException: save2.sav (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at Game.LoadSlot2(Game.java:400)
at Game.processCommand(Game.java:190)
at Game.play(Game.java:138)
at Main.main(Main.java:20)
java.io.FileNotFoundException: save3.sav (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at Game.LoadSlot3(Game.java:441)
at Game.processCommand(Game.java:192)
at Game.play(Game.java:138)
at Main.main(Main.java:20)
]

Does anyone know what I am doing wrong and/or have a solution to the problem?

Well...the brackets didn't work haha oh well. I hope you can still understand the code.

Is This A Good Question/Topic? 0
  • +

Replies To: [Error/Problem] Saving/Loading Files in text-based game

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: [Error/Problem] Saving/Loading Files in text-based game

Posted 24 April 2012 - 08:15 PM

Quote

Well...the brackets didn't work haha oh well.


No: Not 'haha oh well'.

Please fix the code tags. It really isn't that hard, just follow the example in the box where you pasted the code. If that's an example of how well you follow even simple examples, then its no wonder you're having issues.

:code:

Attached Image


Quote

I am trying to create a save/load file so that you can track what room you are in, items obtained, what rooms you have been in, etc. Does anyone know what I am doing wrong


"D:/MyDocuments/EclipsePrjects/Labyrinthian/saves/save1.sav"

Could it be that you misspelled "Projects" ??
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1