我製作了自己的緩衝寫入器。但我不知道它是否確實如此?DataInputStream和DataOutputStream
我在註銷時製作了一個緩衝讀取器(200個硬幣),當我登錄時我得到了(545453個硬幣)或其他數量,我確定它是緩衝寫入器,請幫助!
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,它既沒有緩衝也沒有Writer。 – EJP
爲什麼你將'SavePlayer'設置爲'true',然後立即測試'false'?沒有用。 – EJP
而你不需要調用'File.createNewFile()'。調用'new FileOutputStream()'做到這一點。 – EJP