如何使用格式化程序附加到Java中的文件? [英] How to append to a file in java using formatter?
本文介绍了如何使用格式化程序附加到Java中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的问题.我使用这段代码使用Java在文本文件中添加一些记录,但是每次运行此程序时,都会再次创建该文件.我想追加记录而不再次创建文件?
i have a simple problem. i use this code to add some records in text file using java but every time i run this program, the file is being created again. i wanna to append records without creating file again ?
Formatter x = null;
try{
x = new Formatter("C:\\Users\\Hamada\\Downloads\\products.txt");
}
catch(Exception e)
{
//System.out.println("NO Database");
}
x.format("%d,%s,%s,%s,%d,%d,%d,%d,%d,%d,%d",id, name, type, brand, quantity, day1, month1, year1, day2, month2, year2);
x.close();
推荐答案
您将需要将FileStream添加到Formatter,例如以下内容,并将附加项写入Formatter.
You will need to add a FileStream to the Formatter such as the following and write the appendable to the Formatter.
FileWriter f = new FileWriter("C:\\Users\\Hamada\\Downloads\\products.txt", true);
Formatter form = new Formatter(f);
在FileWriter中传递"true"参数以附加到文件,而不覆盖当前内容.
Within the FileWriter pass the "true" parameter to append to the file and not overwrite the current contents.
这篇关于如何使用格式化程序附加到Java中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文