标签:pat demo logs trace append new 分享 iss try
os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)
information:
txt文件初始状态

code:
package jizuiku0;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* @version V17.09
*/
public class FileAppendDemo {
public static void main(String[] args) {
String path = "E:\\test.txt";
FileOutputStream fos = null;
try {
// 如果指定路径的文件不存在的话,就会创建
// 参数列表中 path后面的 true,
fos = new FileOutputStream(path, true);// 可能引发 FileNotFoundException
fos.write("java".getBytes());// 可能引发 IOException
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// fos不为空才执行close方法,用if判断可以避免空指针异常
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("mission success");
}
}
result:
控制台:

文本文件:

API:
Java优秀,值得学习。
学习资源:API手册 + Java源码 + 清净的心地。
JavaSE8基础 FileOutputStream write 写入txt文本时实现数据追加
标签:pat demo logs trace append new 分享 iss try
原文地址:http://www.cnblogs.com/jizuiku/p/7573514.html