2016-06-12 1 views
-1

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

어떤 도움 감사합니다.

답변

0

, 당신은 읽기와 같은 파일에 작성도

String line = null; 
    while ((line = br.readLine()) != null){ 
      // code 
    } 

아래처럼 코드를 변경 정의, 당신은 읽고 쓸 수 없습니다 동시에 동일한 파일에. 원하는 경우 임시 파일에 쓸 수 있으며 나중에 이름을 바꿀 수 있습니다. 같은 파일에 쓰기 원하는 경우

, 당신은 내가 당신과 내 코드를 대체

 BufferedReader br = new BufferedReader(new FileReader("users.txt")); 
     String line = null; 
     StringBuilder sb = new StringBuilder(); 
     String zlepek = ""; 

     while ((line = br.readLine()) != null) { 
      zlepek = // logic to get value 
      sb.append(zlepek).append("\n"); 
     } 
     br.close(); 

     PrintWriter pw = new PrintWriter(new FileWriter("users.txt")); // pass true to append 
     pw.print(sb); 
     pw.close(); 
+0

아래 참조 쓰기 전에 독자를 종료해야하지만, 파일은 내가 쓸 후에도 비어 그것. – n3onis

+0

답변이 업데이트되었습니다. 입력 파일에 텍스트가 있습니까? – Saravana

+0

먼저 사용자를 등록하고 "users.txt"파일에 저장합니다. 이 줄은 "username-password-wins-losses-ties"(user-pass-0-0-0)와 같습니다. 그런 다음 게임을하고이 코드는 변경되고 변경되지 않은 모든 줄을 "zlepek"문자열에 넣어야합니다. 결국, "zlepek"문자열에있는 내용을 파일에 붙여 넣어 기존 텍스트를 덮어 씁니다. 대신 모든 것을 빈 파일로 바꿉니다. – n3onis

0

while(br.readLine()!=null) 문제는이 행을 읽었지만 저장하지 않았기 때문에 그 행이 어디인지 알 수 없으므로 행을 읽고 저장하지 않으면 행이 항상 비어있게됩니다.

String line = br.readLine();을 제거하고 잠시 후에 읽으십시오. (가) String line;while((line = br.readLine()) != null){ // do something}

당신은 첫 번째 줄에 모든 홀수 라인을 건너 뛰는
0
try { 
     String[] tab = null; 
     String line = null; 
     tab = line.split("-"); 
     int countLoss = Integer.parseInt(tab[3]+plusLoss); 
     int countWin = Integer.parseInt(tab[2]+plusWin); 
     int countTie = Integer.parseInt(tab[4]+plusTie); 

     BufferedReader br = new BufferedReader(new FileReader("users.txt")); 
     StringBuilder sb = new StringBuilder(); 
     String zlepek = ""; 

     while ((line = br.readLine()) != null) { 
      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; 
       } 
      sb.append(zlepek).append("\n"); 
     } 
     br.close(); 

     PrintWriter pw = new PrintWriter(new FileWriter("users.txt")); // pass true to append 
     pw.print(sb); 
     pw.close(); 

     plusWin = 0; 
     plusLoss = 0; 
     plusTie = 0; 

    } catch(IOException e) {} 
} 
관련 문제