我知道我们应该在我们的问题中添加一些代码,但我真的很傻眼,不能包裹我的头脑或找到任何可以遵循的例子.
基本上我想打开文件C:\ A.txt,其中已有内容,并在结尾处写一个字符串.基本上是这样的.
文件A.txt包含:
John
Bob
Larry
Run Code Online (Sandbox Code Playgroud)
我想打开它并在结尾写Sue所以文件现在包含:
John
Bob
Larry
Sue
Run Code Online (Sandbox Code Playgroud)
很抱歉没有代码示例,今天早上我的大脑已经死了......
Add*_*ted 34
请搜索由Larry Page和Sergey Brin给予世界的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();
}
}
Run Code Online (Sandbox Code Playgroud)
Hov*_*els 11
建议:
true允许将文本附加到文件中(如果存在).println(...)PrintWriter,将新文本写入文件.close()该PrintWriter的应该是在尝试的finally块.例如,
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();
}
}
Run Code Online (Sandbox Code Playgroud)
容易,不是吗?
| 归档时间: |
|
| 查看次数: |
125350 次 |
| 最近记录: |