2012-03-08 2 views
1

내가 여기 튜토리얼을 따라 작동하지 쓰기자바의 FileOutputStream은

File scoreFile = new File("score.txt"); 
if(!scoreFile.exists()) { 
    scoreFile.createNewFile(); 
} 
oFile = new FileOutputStream(scoreFile, false); 
oFile.write("Score = 1".getBytes()); 
oFile.flush(); 
oFile.close(); 

을하지만 아무것도 파일 score.txt에 기록되지되고있다.

편집 : 전체 기능이 아래와 같습니다 :

// Set win or loose to score.dat. 
    public void setScore(boolean won, boolean reset){ 
     out.println("setScore()"); 
     long timePassed = (timeEnd - timeStart)/1000; // Seconds passed. 
     double[] prevScore = getScore(); // get previous score (Won, Lost). 
     // Create a writer to edit the file. 
     File scoreFile = new File("score.txt"); 
     if(!scoreFile.exists()) { 
      try { 
       scoreFile.createNewFile(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     if(!reset){ 
      if(won){ 
       // Add time to Win values. 
       prevScore[0] += timePassed; 
      } 
      else{ 
       // Add time to Lost values. 
       prevScore[1] += timePassed; 
      } 
      try { 
       FileOutputStream oFile = new FileOutputStream(scoreFile, false); 
       // Write new score. 
       byte[] contentBytes = (String.valueOf(prevScore[0]+" "+prevScore[1])).getBytes(); 
       oFile.write("Bye".getBytes()); 
       oFile.flush(); 
       oFile.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     else{ 
      // If battle ended, delete the scores. 
      FileOutputStream oFile; 
      try { 
       if(!scoreFile.exists()) { 
        scoreFile.createNewFile(); 
       } 
       oFile = new FileOutputStream(scoreFile, false); 
       oFile.write("Error".getBytes()); 
       oFile.flush(); 
       oFile.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

나는이 파일을 작성 볼 수 있기 때문에 파일이 생성된다 알고 있지만, 어떤 텍스트를 채우지 않습니다.

+3

당신은 좋은 score.txt 파일에서 찾고 계십니까? scoreFile.getAbsolutePath()를 sysout으로 시도해보십시오. 이렇게해도 문제가 해결되지 않으면 SSCCE (http://sscce.org) –

+0

게시 이유는 무엇입니까? 콘텐츠를 가져 와서 버리는 이유는 무엇입니까? – gawicks

답변

2

코드 조각이 저에게 효과적입니다 ... 올바른 곳을보고 계십니까? 예를 들어 파일 이름을 "C:\\score.txt"으로 변경하면 올바른 폴더를 볼 수 있습니다.

+0

편집 된 질문을보고 코드를 수정할만한 것을 제안 해 주시겠습니까? 감사 –

1

코드는 확실히. .Score.txt 작업 디렉토리에 있어야합니다 (당신이 oFile 선언 한 가정) 작동