验证一下:
byte[] a = {123,123,123};//{
byte[] b = {122,122,122};//z
FileInputStream fis = new FileInputStream("/root/3.txt");
FileOutputStream fos = new FileOutputStream("/root/3.txt");
fos.write(a);
fis.read(b);
System.out.println(new String(b,0,3));
上面使用的是不带缓冲的FileInputStream FileOutputStream,输出结果为{{{。因为FileInputStream FileOutputStream不带缓冲,写入数据直接存在文件中,马上读就可以读出。所以不用关闭文件输出流文件内容已经更新。
byte[] a = {123,123,123};//{
byte[] b = {122,122,122};//z
FileInputStream fis = new FileInputStream("/root/3.txt");
FileOutputStream fos = new FileOutputStream("/root/3.txt");
BufferedOutputStream dos=new BufferedOutputStream(fos);
bos.write(a);
fis.read(b);
System.out.println(new String(b,0,3));
上面使用的是不带缓冲的BufferedOutputStream,输出结果为zzz。因为BufferedOutputStream带缓冲,写入数据没有直接存在文件中,不能马上读出。closeBufferedOutputStream之后文件内容才会更新。
byte[] a = {123,123,123,123,123,
123,123,123,123,123,
123,123,123,123,123,
123,123,123,123,123,
123,123,123,123,123,
123,123,123,123,123,
123,123,123,123};//{ 34个
byte[] b = {122,122,122,122,122,
122,122,122,122,122,
122,122,122,122,122,
122,122,122,122,122,
122,122,122,122,122,
122,122,122,122,122,
122,122,122,122
};//z
FileInputStream fis = new FileInputStream("/root/3.txt");
FileOutputStream fos = new FileOutputStream("/root/3.txt");
BufferedOutputStream dos=new BufferedOutputStream(fos,32); //缓冲区大小32Bytes
dos.write(a);
fis.read(b);
System.out.println(new String(b,0,34));
当写满缓冲区后,缓冲区的内容也会写入文件的。