package TestIO;
import java.io.*;
public class Test01 {
public static void main(String[] args) {
FileOutputStream fos=null;
try {
fos = new FileOutputStream("d:/test1.txt",true);//以追加的方式写入文件
String str = "hello word";
byte[] bytes =str.getBytes() ;
fos.write(bytes);
String str1 = "\r\n";//换行
fos.write(str1.getBytes());
fos.flush();//推荐最后的时候为了保证数据完全写入硬盘,所以要刷新
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}finally{
try {
if(fos != null){}
fos.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
187_IO流_FileOutputStream_文件字节输出流_详解
最新推荐文章于 2021-02-21 08:47:45 发布
本文介绍了一个使用Java进行文件操作的例子,演示了如何将字符串以追加的形式写入到指定路径的文本文件中,并确保数据完整写入硬盘。
169

被折叠的 条评论
为什么被折叠?



