Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java DataInputStream和DataOutputStream_Java_Java Io_Datainputstream_Dataoutputstream - Fatal编程技术网

Java DataInputStream和DataOutputStream

Java DataInputStream和DataOutputStream,java,java-io,datainputstream,dataoutputstream,Java,Java Io,Datainputstream,Dataoutputstream,我制作了自己的bufferedwriter。但我不知道它是否真的是这样 我做了一个bufferedreader当我注销时,我有(200个硬币),当我登录时,我得到(545453个硬币)或其他金额,我相信这是bufferedwriter,请帮助 public static int coins; private static final String DIR = "./Data/"; public static boolean SavePlayer; public static v

我制作了自己的bufferedwriter。但我不知道它是否真的是这样

我做了一个bufferedreader当我注销时,我有(200个硬币),当我登录时,我得到(545453个硬币)或其他金额,我相信这是bufferedwriter,请帮助

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;
    }
}


如何使其正确保存所有(整数)?

从这三行中,看起来您正在保存玩家的姓名,然后是硬币计数

DataOutputStream o = new DataOutputStream(fileOutputStream);
o.writeUTF(Player.playerName); 
o.writeInt(Player.coins);
然后试着像这样重新读入它们:

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

Player.playerName = l.toString();

通常,您应该使用类似于
PrintWriter
的东西来编写文件。您不必编写低级操作,如
writeUTF
writeInt
。你可以直接做

printWriter.println(playerName);
阅读时,使用
扫描仪
缓冲阅读器

这是错误的:

Player.playerName = l.toString();
您没有从
DataInputStream
中读取任何数据,您只是将
DataInputStream
对象转换为字符串。调用
readUTF()
而不是
toString()


使用Eclipse与代码是否正确无关。我会把我的答案修改得更清楚些。你完全没有领会我的意思。您使用
o.writeUTF()
保存。您应该使用
l.readUTF()
进行回读。没有对应于
l.toString()
o.fromString()
,因此您“修复”了它的错误。再试一次。所有这些答案都告诉你同样的事情,而且它们都是正确的(到目前为止)。您应该能够整理自己的语法错误。也许您想在问题中添加一条确切的错误消息,或者您遇到的确切现象?你之前说过你肯定是作者而不是读者,但我只是在读者中发现了一个bug。你怎么知道失败的是作者?您正在检查输出文件的十六进制转储吗?注意:StackOverflow不是讨论论坛。答案上的注释不是讨论代码的合适地方。什么缓冲编写器?什么缓冲区?什么作家?这里只有一个DataOutputStream,它既不是缓冲区也不是写入程序。为什么要将“SavePlayer”设置为“true”,然后立即测试它是否为“false”?这是毫无意义的。而且您不需要调用
File.createNewFile()
。调用
newfileoutputstream()
就可以做到这一点。我刚刚开始如何编写和加载文件:$@我想用户2734151加载和读取是一样的。
Player.playerName = l.toString();
Player.playerName = l.readUTF();