I made my own bufferedwriter which works. But I don't know if it really does?
I made a bufferedreader when I logout I have (200 coins) and when I login I get (545453 coins) or other amount I am sure it's the bufferedwriter, PLEASE HELP!
public static int coins;
private static final String DIR = "./Data/";
public static boolean SavePlayer;
public static void saveAll() {
SavePlayer = true;
if (!SavePlayer) {
System.out.println("[WoG Error]: Their was an error saving players.");
return;
}
saveGame();
}
public static boolean saveGame() {
if (Player.playerName == null) {
return false;
}
try {
File file = new File(DIR + Player.playerName.toLowerCase() + ".dat");
if (!file.exists())
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
DataOutputStream o = new DataOutputStream(fileOutputStream);
o.writeUTF(Player.playerName);
o.writeInt(Player.coins);
//o.writeInt(Player.height);
o.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean loadGame() throws InterruptedException {
try {
File file = new File(DIR + Player.playerName.toLowerCase() + ".dat");
if (!file.exists()) {
System.out.println("[WoG Error] Sorry but the account does not exist.");
return false;
}
FileInputStream fileInputStream = new FileInputStream(file);
DataInputStream l = new DataInputStream(fileInputStream);
Player.playerName = l.toString();
Player.coins = l.readInt();
//Player.height = l.readInt();
l.close();
fileInputStream.close();
Player.home();
} catch (final IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}
How do I make it save all (integers) correctly?
From these 3 lines, it looks like you are saving the player's name and then coin-count ...
and then trying to read them back in again like this:
I notice that you are using
l.toString()
instead ofl.readUTF()
.Surely you need to read back data with the corresponding method to that which saved it?
In other words, if you save data with
o.writeUTF()
you need to read back data withl.readUTF()
.Like for like.