Update FileSaving.java

changes to how we deal with saving when it comes to the terminal
This commit is contained in:
Vikramb987
2019-03-25 13:18:16 -06:00
committed by GitHub
parent 77e43f0fd2
commit 19134408a8

View File

@@ -13,6 +13,7 @@ public class FileSaving extends Player implements Serializable {
* @return player * @return player
*/ */
public Player loadFile() { public Player loadFile() {
try { try {
InputStream in = new FileInputStream(new File("src/saves/playerSave.txt")); InputStream in = new FileInputStream(new File("src/saves/playerSave.txt"));
ObjectInputStream inObject = new ObjectInputStream(in); ObjectInputStream inObject = new ObjectInputStream(in);
@@ -22,9 +23,19 @@ public class FileSaving extends Player implements Serializable {
return player; return player;
} }
catch (Exception e) { catch (Exception e) {
try {
InputStream in = new FileInputStream(new File("saves/playerSave.txt"));
ObjectInputStream inObject = new ObjectInputStream(in);
Player player = (Player) inObject.readObject();
in.close();
inObject.close();
return player;
}
catch(Exception e2){
return null; return null;
} }
} }
}
public boolean saveFile(Player player){ public boolean saveFile(Player player){
try{ try{
@@ -39,9 +50,20 @@ public class FileSaving extends Player implements Serializable {
return true; return true;
} }
catch (Exception e) { catch (Exception e) {
try {
FileOutputStream out = new FileOutputStream(new File("saves/playerSave.txt"));
ObjectOutputStream outObject = new ObjectOutputStream(out);
outObject.writeObject(player);
out.close();
outObject.close();
return true;
}
catch(Exception e2){
return false; return false;
} }
} }
}
} }