0
FileWriterクラスとBufferedWriterクラスに問題があります。宣言、初期化、および書き込みメソッドの呼び出し後、作成されるファイルには内容はありません。私はデバッグモードを実行して、toString関数が完全に動作していることを確認し、有効なStringを返しています。私は何が欠けていますか?FileWriterとBufferedWriterがファイルに書き込まない
public String toString(){
String accountInfo = "";
//creates a String of the account info according to an easily parsed
//format
for (int counter = 0; counter < accounts.size(); counter++){
if (counter == 0){
accountInfo = accounts.get(counter).getInfo();
} else{
accountInfo += "\n" + accounts.get(counter).getInfo();
}
}
return accountInfo;
}
public void saveToFile(){
File customer = new File( name + ".txt");
try {
FileWriter custWriter = new FileWriter(customer);
BufferedWriter custBuffer = new BufferedWriter(custWriter);
custBuffer.write(this.toString());
//commented out when I thought I needed to use a BufferedWriter
//custWriter.write(this.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
それをしました。助けてくれてありがとう。 – Benjamin
FileWriterにはclose()が必要です。同じように? BufferedWriterを使用していない場合。 – Benjamin
あなたが書いているライターは、近くにいる必要があります。 – GhostCat