如何在 Java 中向现有文件添加新的文本行? [英] How to add a new line of text to an existing file in Java?

查看:20
本文介绍了如何在 Java 中向现有文件添加新的文本行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不删除该文件的当前信息的情况下向现有文件添加新行.简而言之,这是我使用当前时间的方法:

I would like to append a new line to an existing file without erasing the current information of that file. In short, here is the methodology that I am using the current time:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;

Writer output;
output = new BufferedWriter(new FileWriter(my_file_name));  //clears file every time
output.append("New Line!");
output.close();

上面几行的问题只是它们删除了我现有文件的所有内容,然后添加了新的行文本.

The problem with the above lines is simply they are erasing all the contents of my existing file then adding the new line text.

我想在文件内容的末尾附加一些文本而不删除或替换任何内容.

I want to append some text at the end of the contents of a file without erasing or replacing anything.

推荐答案

你必须以追加模式打开文件,这可以通过使用FileWriter(String fileName, boolean append) 构造函数来实现.

you have to open the file in append mode, which can be achieved by using the FileWriter(String fileName, boolean append) constructor.

output = new BufferedWriter(new FileWriter(my_file_name, true));

应该做的伎俩

这篇关于如何在 Java 中向现有文件添加新的文本行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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