[英]how to write to a text file without overwriting?
I am in a deadlock as to how to write the attributes in a form to a text file in java. 关于如何将表单中的属性写入Java中的文本文件,我陷入了僵局。 I know there are many answers to this but i couldn't find any solution to the problem that i am facing. 我知道对此有很多答案,但我找不到所面临问题的任何解决方案。 While there are many attributes in a form when i try writing them to a file it gets overwritten. 当我尝试将它们写入文件时,表单中有许多属性,但它会被覆盖。 Eg: there are 3 attributes when i try writing them to a file the third one overwrites the second one. 例如:当我尝试将它们写入文件时,有3个属性,第三个覆盖第二个。
Here's what i have done so far: 到目前为止,这是我所做的:
writer = new PrintWriter(new FileWriter("C:/Documents and Settings/Administrator/Desktop/example1.txt"),true);
if(specific attr found)
writer.println("my data");
writer.flush();
if(another found)
writer.println("my data");
writer.flush();
if(third attr found)
writer.println("my data");
writer.flush();
writer.close();
but its not working. 但它不起作用。
You can use FileUtils CommonsIO 2.4 API 您可以使用FileUtils CommonsIO 2.4 API
Example 例
package com.doc;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("test.txt");
FileUtils.write(file, "\n my data", true);
FileUtils.write(file, "\n my data", true);
FileUtils.write(file, "\n my data", true);
}
}
output 产量
my data
my data
my data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.