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. HttpClient——连接关闭

    HttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://www.apache.org& ...

  2. Java 编译报错:illegal character

    1.检查编译版本:1.5还是1.6 2.重新引用一下Jar包

  3. SVN 管理

    01. 源代码管理工具概述(PPT) ================================================================================ ...

  4. 设置trace SQL

    Select PeopleTools, Utilities, Debug, Trace SQL to access the Trace SQL page. You use this page to c ...

  5. 未能加载文件或程序集“Oracle.Web, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342”或它的某一个依赖项

    当前系统环境描述: Win7x64+VS2012+IIS7 当前情况描述: 发布Web服务,在浏览的时候出现以下问题:未能加载文件或程序集“Oracle.Web, Version=2.112.1.0, ...

  6. 定时重启Apache与MySQL方法

    可以定时重启apache服务器等.让网站运行的效果更快. 采用at命令添加计划任务. 有关使用语法可以到window->“开始”->运行“cmd”->执行命令“at /”,这样界面中 ...

  7. Linux rabbitmq的安装和安装amqp的php插件

    RabbitMQ是一个消息代理.它的核心原理非常简单:接收和发送消息.你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件投递到你的收件人处.在这个比喻中,RabbitMQ是一个邮箱.邮局.邮 ...

  8. .ascx.g.cs文件不能生成 The name ‘InitializeControl’ does not exist in the current context - Visual Web part Sharepoint

    InitializeControl doesn't exsit When using visual studio 2012 for developing SharePoint 2013 Visual ...

  9. delphi 更改不了窗体的标题

    delphi定义变量名千万要注意,不能和关键字同名,今天我无意间定义了一个caption的变量  导致我怎么都不能修改窗的标题.

  10. ADO.NET 结构 集中数据库联接结构

    MSDN 原文出处 https://msdn.microsoft.com/zh-cn/library/27y4ybxw.aspx .NET Framework 4.6 and 4.5 其他版本 以前, ...