英文:
How to add data without overwriting the existing data?
问题
我正在创建一个每日储蓄记录应用程序。
我使用的文件会覆盖现有数据。
我需要一个解决方案,可以在不覆盖现有数据的情况下更新文件。
以下是我正在处理的代码:
写入数据的部分:
private static void write(int cur_bal, int amt, int flag) throws IOException
{
File file = new File("bal.txt");
log = new BufferedWriter(new FileWriter("log.txt")); // 日志的写入器
bal = new BufferedWriter(new FileWriter(file)); // " " 余额
Scanner File = new Scanner(file);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
String stat = "";
int balance = 0;
switch (flag)
{
case 1:
{
stat = "Added";
balance = cur_bal + amt;
break;
}
case 2:
{
stat = "Removed";
balance = cur_bal - amt;
break;
}
}
String inf = sdf.format(date) + " " + amt + " (" + stat + ")";
System.out.println(inf);
System.out.println(balance);
bal.write(balance + "\n");
log.write(inf + "\n");
bal.close();
log.close();
}
读取文件的部分:
private static int bal_read() throws IOException
{
FileReader fr = new FileReader("bal.txt");
int i, balance = 0;
while ((i = fr.read()) != -1)
balance = i;
fr.close();
return balance;
}
英文:
I am creating a daily savings log application.
the files I am using overwrites existing data.
I need a solution in which the files can be updated without overwriting existing data.
Here's the code I'm working on;
for writing the data:
private static void write(int cur_bal,int amt ,int flag) throws IOException
{
File file = new File("bal.txt");
log= new BufferedWriter(new FileWriter("log.txt"));//writer for log
bal= new BufferedWriter(new FileWriter(file));// " " bal
Scanner File = new Scanner(file);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
String stat ="";
int balance=0;
switch(flag)
{
case 1:
{stat="Added";
balance = cur_bal+amt;
break;}
case 2:
{stat="Removed";
balance = cur_bal-amt;
break;}
}
String inf=sdf.format(date)+" "+amt+" ("+stat+")";
System.out.println(inf);
System.out.println(balance);
bal.write(balance+"\n");
log.write(inf+"\n");
bal.close();
log.close();
}
for reading the file:
private static int bal_read() throws IOException
{
FileReader fr=new FileReader("bal.txt");
int i,balance=0;
while((i=fr.read())!=-1)
balance = i;
fr.close();
return (balance);
}
答案1
得分: 2
BufferedWriter如果在追加模式下创建FileWriter(new FileWriter(file, true)
),可以向文件中追加内容:
英文:
BufferedWriter can append to the file if the FileWriter is created in append mode: new FileWriter(file, true)
答案2
得分: 2
一个解决方案 -
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("file.txt", true)));
pw.println("一些文本");
pw.close();
另一种方法是创建新的FileWriter(file, true) - 第二个参数true启用了追加模式。
然而,你应该使用像Log4j这样的日志框架来维护日志。
英文:
One solution -
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("file.txt", true)));
pw.println("some text");
pw.close();
Another way is to create new FileWriter(file, true) - The second parameter true enables append mode.
However you should use logging frameworks like Log4j for maintaining logs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论