我想更改用户的分数,但不是用新分数覆盖文件,而只是创建一个空文件。 该行这样写的:“用户名 - 密码 - 胜 - 负 - 关系”PrintWriter写入空白文件
try {
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("users.txt"));
String[] tab = null;
String zlepek = "";
while(br.readLine()!=null) {
String line = br.readLine();
tab = line.split("-");
int countLoss = Integer.parseInt(tab[3]+plusLoss);
int countWin = Integer.parseInt(tab[2]+plusWin);
int countTie = Integer.parseInt(tab[4]+plusTie);
if(tab[0].equals(loginUser.user)) {
String newScore = (tab[0] + "-" + tab[1] + "-" + countWin + "-" + countLoss + "-" + countTie + "\n");
zlepek = zlepek+newScore;
} else {
String oldScore = (tab[0] + "-" + tab[1] + "-" + tab[2] + "-" + tab[3] + "-" + tab[4] + "\n");
zlepek = zlepek+oldScore;
}
}
pw.print(zlepek);
pw.close();
br.close();
plusWin = 0;
plusLoss = 0;
plusTie = 0;
} catch(IOException e) {}
任何帮助表示赞赏。
我代替我的代码与你,但文件仍然是空后,我写的它。 – n3onis
更新回答,你有输入文件中的任何文字吗? – Saravana
首先我注册一个用户,并将其保存到“users.txt”文件中。该行如下所示:“username-password-wins-losses-ties”(user-pass-0-0-0)。然后我玩游戏,这段代码应该将所有已更改和未更改的行都放入“zlepek”字符串中。最后,它应该将“zlepek”字符串中的所有内容粘贴回文件中,覆盖现有文本。相反,它只是用空白文件替换所有内容。 – n3onis