FileOutPutStream继承OutputStream,并不提供flush()方法的重写所以无论内容多少write都会将二进制流直接传递给底层操作系统的I/O,flush无效果。而Buffered系列的输入输出流函数单从Buffered这个单词就可以看出他们是使用缓冲区的。应用程序每次IO都要和设备进行通信,效率很低,因此缓冲区为了提高效率,当写入设备时,先写入缓冲区,每次等到缓冲区满了时,就将数据一次性整体写入设备,避免了每一个数据都和IO进行一次交互,IO交互消耗太大。

使用flush()和不使用flush()效果对比

不使用flush()

  String s = "Hello World";
try {
// create a new stream at specified file
PrintWriter pw = new PrintWriter(System.out);
// write the string in the file
pw.write(s);
// // flush the writer
// pw.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
输出:

buffer没有满,输出为空。

使用buffer()

   String s = "Hello World";
try {
// create a new stream at specified file
PrintWriter pw = new PrintWriter(System.out);
// write the string in the file
pw.write(s);
// flush the writer
pw.flush();
} catch (Exception ex) {
ex.printStackTrace();
}

得到期望的输出结果。

解析

close()和flush()作用有交集!

public static void main(String[] args) {
BufferedWriter fw =null;
try {
fw = new BufferedWriter(new FileWriter("e:\\test.txt"));
fw.write("wo shi lucky girl.");
//fw.flush();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//fw.flush();这句有和无并不影响输出结果,不太明白词句是否必要?

因为close的时候,会把你没flush掉的一起flush掉。缓冲区中的数据保存直到缓冲区满后才写出,也可以使用flush方法将缓冲区中的数据强制写出或使用close()方法关闭流,关闭流之前,缓冲输出流将缓冲区数据一次性写出。在这个例子中,flash()和close()都使数据强制写出,所以两种结果是一样的,如果都不写的话,会发现不能成功写出

Java默认缓冲区大小是多少?

默认缓冲去大小8192字节。

实验

char[] array  = new char[8192];
Arrays.fill(array,'s');
PrintWriter pw = new PrintWriter(System.out);
pw.write(array);
output: char[] array = new char[8193];
Arrays.fill(array,'s');
PrintWriter pw = new PrintWriter(System.out);
pw.write(array);
output:
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss..一共8193个s...sssssssssssssssssssssssssssssssssssssssssssssss

当设置数组长度为8192时没有输出,设置8193时有输出。

经典问题

数据重复问题

Java缓冲流细节的更多相关文章

  1. java 缓冲流 Buffer

    缓冲流 Buffer :设置缓冲区加快执行效率 子类: (一)BufferedInputStream : 缓冲输入字节流 ,目的:提高读取文件的效率  注意: BufferedInputStream ...

  2. Java缓冲流高效大文件的复制实例

    public class BufferedDemo { public static void main(String[] args) throws FileNotFoundException { // ...

  3. Java缓冲流的优点和原理

    不带缓冲的流的工作原理: 它读取到一个字节/字符,就向用户指定的路径写出去,读一个写一个,所以就慢了. 带缓冲的流的工作原理: 读取到一个字节/字符,先不输出,等凑足了缓冲的最大容量后一次性写出去,从 ...

  4. java 缓冲流

    english.txt The arrow missed the target. They rejected the union demand. Where does this road go to? ...

  5. Java缓冲流写出数据实例

    public class BufferedWriterDemo throws IOException { public static void main(String[] args) throws I ...

  6. java的 IO流之缓冲流(转载)

    java缓冲流本身不具IO功能,只是在别的流上加上缓冲提高效率,像是为别的流装上一种包装.当对文件或其他目标频繁读写或操作效率低,效能差.这时使用缓冲流能够更高效的读写信息.因为缓冲流先将数据缓存起来 ...

  7. java IO之 File类+字节流 (输入输出 缓冲流 异常处理)

    1. File类

  8. JAVA基础学习day20--IO流二-缓冲流、字节流

    一.缓冲流 1.1.字符流的缓冲区 缓冲区的出现是为了提高IO的读写效率 对应类 BufferedReader BufferedWriter 缓冲区要结合流才可以使用 在流的基础上对流的功能进行了增强 ...

  9. java 21 - 6 字符缓冲流的特殊方法以及该方法高效复制文件

    字符缓冲流的特殊方法: A.BufferedWriter: public void newLine():根据系统来决定换行符 private static void write() throws IO ...

随机推荐

  1. Jquery获取selelct选中值

    误区: 一直以为jquery获取select中option被选中的文本值,是这样写的: $("#s").text();  //获取所有option的文本值 实际上应该这样: $(& ...

  2. debug版本和release版本的区别?

    好久没有做web项目了,这项目完成了要发布网站,不好忘了 以前操作过的? 还好脑子还是有点印象 现还是 写个文档吧记录吧 免得 以后作别的了又忘了 那可不妙啊 网站发布步骤:1.先将

  3. CentOS 5.x版本升级Mysql

    #-----------------------------CentOS 5.x版本升级Mysql ------------------#! /bin/sh #1.关闭selinuxcp -rp /e ...

  4. Session为null 问题

    问题描述: var svode=HttpContext.Current.Session["VCode"].ToString(); //调试时候发现 svode ==null // ...

  5. ElasticSearch部署

    安装jdk1.7 1.卸载Liunx自带的openjdk rpm -qa | grep jdk 查看当前的jdk版本 sudo yum -y remove java-1.7.0-openjdk-hea ...

  6. Android的ADT内容助手快捷方式设置

    请注明出处:http://www.cnblogs.com/killerlegend/p/3550019.html  Written By KillerLegend 先将Word Completion的 ...

  7. js控制文本框只能输入数字 及 常用字符对应ASCII码值

    方法一: <INPUT TYPE='text' NAME=text onkeypress="a()"><script language=javascript> ...

  8. C# 基础 计算平均值的方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. Java 第六天 Spring Annotation 和其它

    Annotation,是Java语言中的一种特殊的元数据语法,Spring支持使用annotation来进行对象实例化和装配 使用Annotation在Spring的配置xml中添加context命名 ...

  10. ok6410内存初始化

    •DRAM:它的基本原件是小电容,电容可以在两个极板上保留电荷,但是需要定期的充电(刷新),否则数据会丢失.缺点:由于要定期刷新存储介质,存取速度较慢. •SRAM:它是一种具有静止存取功能的内存,不 ...