我知道我们应该在我们的问题中添加一小段代码,但我真的很困惑,我不能包裹我的头,也找不到任何可供参考的例子。
基本上,我想打开文件C:\A.txt,其中已经有内容,并在末尾写一个字符串。基本上是这样的。
文件A.txt包含:
John
Bob
Larry
我想打开它并在最后编写Sue,这样文件现在就包含了:
John
Bob
Larry
Sue
很抱歉没有代码示例,今天早上我的大脑死了....
发布于 2012-05-20 02:32:35
请搜索拉里·佩奇和谢尔盖·布林给世界的Google。
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();
}
}
发布于 2012-05-20 02:22:39
建议:
true
允许将文本附加到文件中,则使用后者。然后,println(...)
,将新文本写入文件。例如,
PrintWriter pw = null;
try {
File file = new File("fubars.txt");
FileWriter fw = new FileWriter(file, true);
pw = new PrintWriter(fw);
pw.println("Fubars rule!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
}
很简单,不是吗?
发布于 2012-05-20 02:27:44
要扩展Eels先生的评论,您可以这样做:
File file = new File("C:\\A.txt");
FileWriter writer;
try {
writer = new FileWriter(file, true);
PrintWriter printer = new PrintWriter(writer);
printer.append("Sue");
printer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
别说我们对你不好!
https://stackoverflow.com/questions/10667734
复制相似问题