Java文件 - 打开文件并写入它 [英] Java File - Open A File And Write To It

查看:154
本文介绍了Java文件 - 打开文件并写入它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们应该在我们的问题中添加一段代码,但我非常傻眼,无法包裹我的头脑或找到任何可以遵循的例子。

I know we are supposed to add a snipit of code to our questions, but I am seriously dumbfounded and can't wrap my head or find any examples to follow from.

基本上我想打开已经包含内容的 C:\A.txt 文件,并在最后写一个字符串。基本上就是这样。

Basically I want to open file C:\A.txt , which already has contents in it, and write a string at the end. Basically like this.

文件A.txt包含:

John
Bob
Larry

我想打开它并在结尾写Sue所以现在文件包含:

I want to open it and write Sue at the end so the file now contains:

John
Bob
Larry
Sue

很抱歉没有代码示例,今天早上我的大脑已经死了....

Sorry for no code example, my brain is dead this morning....

推荐答案

请搜索Larry Page和Sergey给世界的 Google Brin。

Please Search Google given to the world by Larry Page and Sergey Brin.

BufferedWriter out = null;

try {
    FileWriter fstream = new FileWriter("out.txt", true); //true tells to append data.
    out = new BufferedWriter(fstream);
    out.write("\nsue");
}

catch (IOException e) {
    System.err.println("Error: " + e.getMessage());
}

finally {
    if(out != null) {
        out.close();
    }
}

这篇关于Java文件 - 打开文件并写入它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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