사용자 점수를 변경하고 싶지만 파일을 새 점수로 덮어 쓰지 않고 빈 파일을 만듭니다. 선은 다음과 같이 기록됩니다 "사용자 이름 - 암호 승리 - 손실 넥타이"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