2012-02-24 3 views
1

이 문자열을 파일 목록과 함께 파일에 저장하려하지만 마지막 하나만 저장합니다. 여기에있는 문제는 무엇입니까? :(파일 목록을 .txt로 저장합니다.

public void fileprinter() throws IOException{  
    File dir = new File("c:"); 
    String[] children = dir.list(); 
    if (children == null) { 
     } else { 
      for (int i=0; i<children.length; i++) { 
       String filename = new StringBuffer().append(children[i]).toString(); 
       System.out.println(filename); 
       Writer output; 
       File file = new File("D:/file.txt"); 
       output = new BufferedWriter(new FileWriter(file)); 
       output.write(filename); 
       output.close(); 
     } 
    } 
} 

답변

4

당신은 그렇게 마지막 라인은 "생존". 엽니 다 BufferedWriter의

루프의 외부 (한 번!)를 완료하고 그것을 닫습니다, 루프에서 같은 파일을 덮어 보관하십시오.

다른 방법으로 추가 모드에서 열 수 있지만 반복해도 동일한 파일을 반복해서 다시 열지 마십시오.

관련 문제