java 在java中将字符串写入文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34355368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:48:20  来源:igfitidea点击:

Write String to text-file in java

javajsonoverwritefile-writing

提问by anderssinho

Wanna save some information that I parse from a JSON to a plain text into a file and I also want this information to not be overwritten every time you run the program. It's suppose to work as a simple error logging system.

想要将我从 JSON 解析为纯文本的一些信息保存到一个文件中,并且我还希望每次运行程序时这些信息都不会被覆盖。它应该作为一个简单的错误日志系统工作。

So far have I tried this:

到目前为止,我试过这个:

FileWriter fileWriter = null;
File file = new File("/home/anderssinho/bitbucket/dblp-article-analyzer/logg.txt");

// if file doesn't exists, then create it
if (!file.exists()) {
    file.createNewFile();
}

...

String content = "------------------------------------";
fileWriter = new FileWriter(file);
fileWriter.write(content);
//fileWriter.write(obj.getString("title"));
//fileWriter.write(obj.getString("creators"));
//fileWriter.write(article.GetElectronicEdition());

But when I do this it seems that I overwrite the information all the time and I'm also having problem to save the information I wanna grab from the JSON-array that I've got.

但是当我这样做时,我似乎一直在覆盖信息,而且我也无法保存我想从我拥有的 JSON 数组中获取的信息。

How can I do to make this work?

我该怎么做才能使这项工作?

回答by optimus

FileWriter fooWriter = new FileWriter(myFoo, false);

FileWriter fooWriter = new FileWriter(myFoo, false);

// true to append
// false to overwrite; where myFoo is the File name

// true 追加
// false 覆盖;其中 myFoo 是文件名

See this link

看这个链接

回答by KD157

Can you be more elaborate on this? If the problem is just not able to append then you can just add an argument to the FileWriter saying it to append and not write from the beginning.

你能更详细地说明这一点吗?如果问题只是无法追加,那么您可以向 FileWriter 添加一个参数,说它追加而不是从头开始写入。

Check the constructor here:

在此处检查构造函数:

public FileWriter(String fileName, boolean append) throws IOException

Official Java Documentation:

官方 Java 文档

Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

Parameters:
fileName - StringThe system-dependent filename.
append - booleanif true, then data will be written to the end of the file rather than the beginning.

Throws:
IOException- if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason

构造一个 FileWriter 对象,给定一个带有布尔值的文件名,指示是否附加写入的数据。

参数:
fileName - String系统相关文件名。
append - boolean如果为 true,则数据将写入文件的末尾而不是开头。

抛出:
IOException- 如果指定的文件存在但它是一个目录而不是常规文件,不存在但无法创建,或者由于任何其他原因无法打开

回答by guillaume girod-vitouchkina

use append:

使用附加:

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("logg.txt", true)));

see this: How to append text to an existing file in Java

请参阅: 如何将文本附加到 Java 中的现有文件