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

查看:30
本文介绍了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 包含:

File A.txt contains:

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....

推荐答案

请搜索 Google 给予世界拉里·佩奇和谢尔盖·布林.

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("
sue");
}

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

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

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

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