-3

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?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 2
    What buffered writer? What buffer? What Writer? All you have here is A DataOutputStream, which is neither buffered nor a Writer. – user207421 Aug 30 '13 at 20:09
  • 1
    And why are you setting 'SavePlayer' to 'true' and then immediately testing it for 'false'? It's pointless. – user207421 Aug 30 '13 at 20:38
  • And you don't need to call `File.createNewFile()`. Calling `new FileOutputStream()` does that. – user207421 Sep 09 '13 at 00:01

3 Answers3

5

From these 3 lines, it looks like you are saving the player's name and then coin-count ...

DataOutputStream o = new DataOutputStream(fileOutputStream);
o.writeUTF(Player.playerName); 
o.writeInt(Player.coins);

and then trying to read them back in again like this:

DataInputStream l = new DataInputStream(fileInputStream);
Player.playerName = l.toString(); // <-- change to l.readUTF()
Player.coins = l.readInt();

I notice that you are using l.toString() instead of l.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 with l.readUTF().

Like for like.

Stewart
  • 17,616
  • 8
  • 52
  • 80
  • I'mnot sure I am using Eclipse, and l.toString was the first option. But the playername(carlos) saves on the .dat file. – user2734151 Aug 30 '13 at 20:06
  • 1
    Using Eclipse has nothing to do with whether the code is correct or not. I'll edit my answer to be more clear. – Stewart Aug 30 '13 at 20:07
  • No.. my code was this: Player.playerName = l.String(); and I got an error then Eclipse told me to change it to Player.playerName = l.toString(); – user2734151 Aug 30 '13 at 20:08
  • 2
    You missed my point completely. You saved using `o.writeUTF()`. You should read back with `l.readUTF()`. There is no `o.fromString()` which corresponds to `l.toString()` – Stewart Aug 30 '13 at 20:09
  • Ok I did it and fixed it, but It doesn't save? – user2734151 Aug 30 '13 at 20:18
  • 1
    So you 'fixed' it wrong. Try again. All these answers are telling you the same thing, and they are all correct (so far). You are expected to be able to sort out your own syntax errors. – user207421 Aug 30 '13 at 20:26
  • 1
    Perhaps you want to add to your question an exact error message, or exact phenomena that you're running into? You said before that you're sure it's the writer and not the reader, but I just found a bug in the reader. How to do you *know* it's the writer that's failing? Are you examining a hex dump of the output file? – Stewart Aug 30 '13 at 20:26
  • @Stewart I would remove the stuff about 'fromString()' from this answer: it's just confusing. Stick to writeXXX() => readXXX(). – user207421 Aug 30 '13 at 20:29
  • @EJP - done. I was running out of ideas for how to explain it :-) – Stewart Aug 30 '13 at 20:31
  • It doesn't save right, the .dat file creates but when I login it says you have 200 coins, when everytime i login It gives me 200 coins? – user2734151 Aug 30 '13 at 20:32
  • 1
    @user2734151 I can't make head or tail of that. You aren't explaining yourself clearly. And there isn't enough information in your question either. What is the definition of the Player class? Why is 'coins' static? What does login have to do with it? We can't read your mind, or the code you haven't posted. – user207421 Aug 30 '13 at 20:35
  • in my login I added. Player.coins += 200; System.out.println("you have: " +Player.coins); o it says 200. And before I after the 200; I added PlayerSave.saveAll(); <- saves the player (bufferedwriter), and when I exit the cmd I login it says i still have 200, when Player.load is after the PlayerSave.saveAll – user2734151 Aug 30 '13 at 20:45
  • 2
    Note: StackOverflow is not a discussion forum. The comments on an answer are not a suitable place to discuss your code. – Jesper Aug 30 '13 at 20:49
  • 1
    With statements such as "before I after the 200" you're making no sense at all. You are mentioning far too many factors (moving parts) to the problem, and not being explicit about much except your save/read routines. If you do `Player.coins += 200;` then don't be surprised if then `Player.coins == 200;` Please put more effort into your main question, otherwise no one can help you. Meanwhile, -1 to the question. PS: Use backticks (`) to format code in comments. – Stewart Aug 30 '13 at 20:57
2

Change

Player.playerName = l.toString();

to

Player.playerName = l.readUTF();

Generally you should use something like PrintWriter to write files. You don't have to write low level operations like writeUTF or writeInt. You could directly do

printWriter.println(playerName);

And while reading, either use Scanner or BufferedReader

bsd
  • 2,707
  • 1
  • 17
  • 24
2

This is wrong:

Player.playerName = l.toString();

You are not reading any data from the DataInputStream here, you're just converting the DataInputStream object to a string. Call readUTF() instead of toString():

Player.playerName = l.readUTF();
Jesper
  • 202,709
  • 46
  • 318
  • 350