重新写入java txt文件

发布于 2024-11-26 06:04:17 字数 175 浏览 0 评论 0原文

我想知道是否有一种方法可以添加到已创建的文本文件中。因为当我在一个已经创建的文件上执行此操作时:

public Formatter f = new Formatter("filename.txt");

它会用空白文件重写当前的 filename.txt 。 谢谢,奎因

i was wondering if there was a way to add to text files already created. because when i do this on an already created file:

public Formatter f = new Formatter("filename.txt");

it re-writes the current filename.txt with a blank one.
thanks, Quinn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2024-12-03 06:04:17

是的,请使用带有 OutputStream 参数而不是 File 参数的构造函数。这样您就可以在追加模式下打开 OutputStream 并对其进行格式化。 链接

Yes, use the constructor with an OutputStream argument instead of a File argument. That way you can open an OutputStream in append mode and do your formatting on that. Link

段念尘 2024-12-03 06:04:17

尝试使用 Formatter 的构造函数,它采用 Appendable 作为参数。

有几个类实现了 Appendable 接口。根据您的情况,最方便的应该是 FileWriter

这个 FileWrite 构造函数 将让您以附加模式打开一个文件(其名称指定为字符串)。

Try using the constructor for Formatter which takes an Appendable as an argument.

There are several classes which implement the Appendable interface. The most convenient, in your case, should be FileWriter.

This FileWrite constructor will let you open a file (whose name is specified as a String), in append mode.

醉南桥 2024-12-03 06:04:17

使用 FileOutputStream 并将附加布尔值设置为 true 例如 new FileOutputStream("C:/concat.txt", true));

示例

public class FileCOncatenation {

        static public void main(String arg[]) throws java.io.IOException {
                PrintWriter pw = new PrintWriter(new FileOutputStream("C:/concat.txt", true));

                File file2 = new File("C:/Text/file2.rxt");
                                       System.out.println("Processing " + file2.getPath() + "... ");
                        BufferedReader br = new BufferedReader(new FileReader(file2
                                        .getPath()));
                        String line = br.readLine();
                        while (line != null) {
                                pw.println(line);
                                line = br.readLine();
                        }
                        br.close();
//                }
                pw.close();
                System.out.println("All files have been concatenated into concat.txt");
        }
}

Use FileOutputStream with append boolean value as true eg new FileOutputStream("C:/concat.txt", true));

Example

public class FileCOncatenation {

        static public void main(String arg[]) throws java.io.IOException {
                PrintWriter pw = new PrintWriter(new FileOutputStream("C:/concat.txt", true));

                File file2 = new File("C:/Text/file2.rxt");
                                       System.out.println("Processing " + file2.getPath() + "... ");
                        BufferedReader br = new BufferedReader(new FileReader(file2
                                        .getPath()));
                        String line = br.readLine();
                        while (line != null) {
                                pw.println(line);
                                line = br.readLine();
                        }
                        br.close();
//                }
                pw.close();
                System.out.println("All files have been concatenated into concat.txt");
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文