-3
テキストをファイルに保存する際に問題があります。テキストフィールドのテキストをファイルに保存しています。テキストフィールドはファイルに保存されますが、プログラムを閉じて新しいエントリを保存しようとすると、ファイルが消去されて破損します。ヘルプをいただければ幸いです:)ファイルにテキストを保存する際に問題が発生しました
// BUTTON SAVE ------------------------------------------
if(e.getSource() == btnSave)
{
// Call the saveEntry method that will copy the current
// TextField entries from the screen to the current
// record in the array in memory.
saveEntry(currentEntry);
}
public void saveEntry(int i) //
{
PersonsInfoData[i].setPersonsInfo(txtPersonsName.getText(),txtLikes.getText(),txtDislikes.getText(), txtBdayDay.getText(), txtBdayMonth.getText());
// You may also wish to write all the records that are currently in the array
// to your data file on the hard drive (USB, SSD, or equivalent)
writeFile(dataFileName);
}
public void writeFile(String fileName)
{
try
{
PrintWriter printFile = new PrintWriter(new FileWriter("BirthdayTracker.txt"));
for(int i = 0; i < numberOfEntries; i++)
{
printFile.println(PersonsInfoData[i].getPersonsName() + "," + PersonsInfoData[i].getPersonsLikes() + "," + PersonsInfoData[i].getPersonsDislikes() + "," + PersonsInfoData[i].getBdayDay() + "," + PersonsInfoData[i].getBdayMonth());
}
printFile.close();
}
catch (Exception e)
{
System.err.println("Error Writing File: " + e.getMessage());
}
'writeFile'に渡す' filename'を使わず、代わりにハードコードされたファイル名を使用することと関係するかもしれません。ファイル以外のファイルが上書きされることは何を期待していますか? – Filburt