DataInputStream and DataOutputStream

1.1k views Asked by At

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?

3

There are 3 answers

14
Stewart On

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.

2
bsd On

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

0
Jesper On

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();