简体   繁体   English

写入txt文件,但不覆盖

[英]Write to txt file, but not overwrite

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.但至少我不知道如何解决它。

I write some lines to a.txt file when I hit the button Save.当我点击保存按钮时,我将一些行写入 a.txt 文件。 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.此外,当我再次关闭并重新启动应用程序并再次点击保存时,它必须再次将文本保存在新行中。

So basically: How can I write text to a.txt file, without overwriting previous lines.所以基本上:如何在不覆盖以前的行的情况下将文本写入 a.txt 文件。 I can write to a file, so that is not the problem, but only how to NOT overwrite.我可以写入一个文件,所以这不是问题,而只是如何不覆盖。

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);
        }
}

Please put me in the good directions.请告诉我正确的方向。

Thanks already, Bigflow已经谢谢了,Bigflow

try:尝试:

public FileWriter (File file, boolean append)

set the append to trueappend设置为true

You have to do你必须要做

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

Just as said in java documentation:正如 java 文档中所说:

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 

Well given this is just a little of your code (and I'm assuming you chunked it out so as to not reveal other parts) what I suspect is going on is that you're opening the file root + "/Save/Contacten.txt" in a non-append mode.好吧,鉴于这只是您的一小部分代码(并且我假设您将其分块以免泄露其他部分)我怀疑正在发生的事情是您正在打开文件 root + "/Save/Contacten. txt" 在非追加模式。 The first time you call it the file is created and written to.第一次调用它时,文件被创建并写入。 Subsequent times you call it, it finds the file, and recreates (or deletes content) and then writes to it.随后您调用它时,它会找到该文件并重新创建(或删除内容),然后写入该文件。

Try using:尝试使用:

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

Of course the first time you open/create the file you'll want it to be false (unless you ALWAYS want to append if the file already exists).当然,第一次打开/创建文件时,您会希望它为 false(除非您总是希望 append 如果文件已经存在)。

Give that a spin.试一试。

you can check for if file exits or not?您可以检查文件是否存在?

or you can also append old file.或者你也可以 append 旧文件。

If not exits then and then only create new one.如果不退出则只创建新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM