2013-04-20 3 views
1

오류 로그를 파일에 쓰는 프로그램을 만들고 있지만 파일을 저장할 때 예외가 발생하지 않습니다. 내가 뭘 잘못 했니?파일 저장시 파일을 저장하지 않습니다.

"저장"버튼의 actionListener :

public void actionPerformed(ActionEvent arg0) { 
    String savePath = getSavePath(); 

    try { 
     saveFile(savePath); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

그리고 세 개의 파일 방법 : 귀하의 getLogFileName(...) 기능은 당신이 그것을 공급 경로에 아무것도되지

private String getSavePath() { 
    JFileChooser fc = new JFileChooser(); 
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 

    fc.showOpenDialog(this); 

    return fc.getSelectedFile().getAbsolutePath(); 

} 

private void saveFile(String path) throws IOException { 
    File outFile = createFile(path); 

    FileWriter out = null; 

    out = new FileWriter(outFile); 

    out.write("Hey"); 

    out.close(); 

} 

private File createFile(String path) { 
    String fileName = getLogFileName(path); 
    while (new File(fileName).exists()) { 
     fileCounter++; 
     fileName = getLogFileName(path); 

    } 

    File outFile = new File(fileName); 
    try { 
     outFile.createNewFile(); 
    } catch (IOException e) { 
     e.printStackTrace(); 

    } 

    return outFile; 

} 

private String getLogFileName(String path) { 
    return "enchantcalc_err_log_" + fileCounter + ".txt"; 
} 
+1

일부 디버그를 만들었 으면 "클릭"액션에 도달 했습니까? – Zakaria

+0

'ActionListener'를'add()'했습니까? – jlordo

답변

3

. 따라서 실제 경로가없는 enchantcalc_err_log_#.txt에 파일을 쓰려고합니다. 대신 다음을 시도하십시오.

private String getLogFileName(String path) { 
    return path + "enchantcalc_err_log_" + fileCounter + ".txt"; 
} 
+0

고마워요! 나는 정말로 혼란 스러웠다. .. 오과 나는 경로와 이름 사이에 분리 기호를 추가했다. – Creator13

+0

나는 그것이 필요할지도 모른다는 느낌이 들었다. – supersam654

2

아마도 파일을 찾을 수 없습니다. 당신의 saveFile의 끝에서

,이 시도 :

out.close(); 

이 같은 라인을 넣어 후 :

out.close(); 
System.out.println("File saved to: "+outFile.getAbsolutePath()); 

당신은 다음이 저장된 신비 경로를 얻을 수 있습니다 .

관련 문제