我有一個文件DataFile.txt幾個記錄。當我添加新條目時,它會清除所有其他記錄並只保存新條目。但我想追加該記錄。java:在現有文件中追加數據
private void saveFile()
{
try
{
PrintWriter out = new PrintWriter(new FileWriter("DataFile.txt"));
String name ="";
String ID="";
String roomType ="";
String meal="";
int days=0;
int tprice=0;
for (int i = 0; i < myList.size(); i++)
{
Customer c = myList.get(i);
name = c.getName();
ID = c.getID();
roomType = c.getRoomItem();
meal = c.getMealItem();
days = c.getDaysIndex();
tprice = c.getTotalPrice();
out.println(name + "," + ID+ "," + roomType+ "," + meal+ "," + days+ "," + tprice);
}
out.close();
JOptionPane.showMessageDialog(null,"Data saved successfully!","",
JOptionPane.INFORMATION_MESSAGE);
}
catch (Exception ex)
{
System.out.println("save file fail");
}
} //end of the method
謝謝。
我想知道它的工作原理,儘管增加了「真實」。 – Ravi 2012-04-25 10:43:30
@Ravi:它不應該 - 應該追加到現有的文件。 – 2012-04-25 10:43:58
我在另一個程序中有一個相同的方法,並且還沒有一個真實的參數,但那個工作非常好。 – Ravi 2012-04-25 10:48:25