写入TXT文件,但不覆盖 [英] Write to txt file, but not overwrite

查看:611
本文介绍了写入TXT文件,但不覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,它看似简单(我个人认为这是),但我找不到答案。但至少我不知道如何解决它。

I got a little problem, it seems simple (personally I think it is), but I couldn't find a answer. But atleast I don't know how to fix it.

我写的一些行到一个txt文件,当我按下按钮保存。
那之后,当我键入其他东西,并再次命中保存,它会覆盖我的第一线。

I write some lines to a .txt file when I hit the button Save. Then after that, when I type something else, and hit Save again, it overwrites my first lines.

不过,我想,它写在一个新行。此外,当我关闭并重新启动应用程序,并创下再次保存,它必须重新保存在新的一行文字。

But I want that it writes at a new line. Also when I close and restart the app again, and hit save again, it must save the text on a new line again.

所以基本上:如何我可以写文本到.txt文件,而不覆盖previous线。
我可以写一个文件,所以这不是问题,而只是如何不覆盖。

So basically: How can I write text to a .txt file, without overwriting previous lines. I can write to a file, so that is not the problem, but only how to NOT overwrite.

这是我的code的小的一部分:

This is the "little" part of my code:

   public void Data_save_contacts(View v) {

        Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

        try {

                writer_contact = new BufferedWriter(new FileWriter(root + "/Save/Contacten.txt"));
                writer_contact.write("Perceel "+str_boer_teler_nummer+" = "+str_boer_teler);
                writer_contact.newLine();   

        } catch (IOException e) {
            System.err.println(e);
        }
}

请把我的好方向。

感谢不已,Bigflow

Thanks already, Bigflow

推荐答案

您需要做的

writer_contact =新的BufferedWriter(新FileWriter的(根+/Save/Contacten.txt\",true));

writer_contact = new BufferedWriter(new FileWriter(root + "/Save/Contacten.txt",true));

正如Java文档中说:

Just as said in java documentation:

FileWriter

public FileWriter(File file,
              boolean append)
       throws IOException

Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.

Parameters:
file - a File object to write to
append - if true, then bytes will be written to the end of the file rather than the beginning 

这篇关于写入TXT文件,但不覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆