格式化程序是否只在關閉流時才寫出來?Java格式化程序輸出
代碼:
public void save(String content, String filename) {
if(filename!=""){
setFileName(filename);
try {
output = new Formatter(new File(fileName));
output.format("%s",content);
} catch (FormatterClosedException ex){
System.err.println("Error writing to file");
return;
} catch (NoSuchElementException ex){
System.err.println("invalid input");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
output.close();
}
起初,我忘了加上最後用output.close(),當該方法得到了所謂的不會寫任何東西阻擋。那麼格式化程序只有在關閉流程時才真正寫入其內容纔是正確的?
這是如何回答這個問題的?當'Formatter'寫入文件時,文檔沒有說明何處。 –
* ...並且被緩衝。*緩衝是許多Java類中的一個概念,並且* always *需要調用'flush'('close'也是這樣)。 – exception1
-1這不是問題的答案。 flush()被close()調用的方面在這裏不是這種情況。這種情況是,當緩衝區不僅關閉時,文件將被寫入。 –