老师,文件字节和字符输出流,都是当缓存区满了之后,才会自动调用flush()进行刷新
我测试了一下字符和字节不关闭,IO流是否能将数据写入文件中
字节不关闭IO流可以,字符不行,必须关闭IO或使用flush()刷新一下
public class Example{
public static void main(String[] args) throws IOException{
FileWriter fil = new FileWriter("D:/a.txt");
char[] arry = new char[] {'s','s'};
fil.write(arry);
// fil.close();
}
}
public class TestFileOutputStream {
public static void main(String[] args) throws IOException {
//搭建通道(程序与目的地)
FileOutputStream fil = new FileOutputStream("D:/keobei2.txt");
//write() 一次写入一个字节
//write(byte[] buf) //将数组中所有内存写入缓存区中
//write(byte[] buf,int off,int len) //指定一次写入多少个字节
//write(String str) //直接写入字符串
//flush() 强制刷新缓存区
//close() 关闭IO流
//注意:写入的数据不是直接写入文件当中,而是写入缓存区当中
//当缓冲满了后自动调用flush()方法,刷新缓存区,将缓存区的
//内容存到文件当中
//一次写入一个字节,以数字形式写入
//fil.write(97);
//一次写入一个字符串
//但是他不能直接字符串,需要转换一次
//fil.write(String.valueOf("你好呀").getBytes());
//一次输入多个字节
byte[] arry = "你好我是你的老师1".getBytes();
// byte[] arry02 = new byte[] {02,52,35,15,124};
fil.write(arry);
//关闭IO流
// if (fil != null) {
// fil.close();
// }
}