如何添加数据而不覆盖现有数据?

huangapple go评论82阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月27日 17:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63613406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定