那么,有人会问使用这种类的时候,难道必须使用flush()方法吗,当然不是喽??!!不过有个前提,你的字节数据必须不能小于8KB。实例代码,注意没有flush()方法。
[java]
package mark.zhang;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("text.txt");
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024*8];
bos.write(b);
//bos.flush();
}
}
[java]
package mark.zhang;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("text.txt");
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024*8];
bos.write(b);
//bos.flush();
}
}
执行代码,会产生8KB的文本文件。当然,怎么可能你每时每刻都知道你的数据一定会不小于8KB呢,所以还是调用flush()方法比较安全。不过,话又说回来,一般用完IO流之后(如果你有一个好的习惯)我们都会去调用close()方法,看源码可以知道该方法也是调用相对应的flush()方法。所以,大多数情况下你不必要担心。这里提醒一下,如果你的文件读写没有达到预期目的,十之八九是因为你没有调用flush()或者close()方法。
另外,字符流类大多数都实现了flush()或者close()方法,只不过,它们调用的是StreamEncoder类的该方法。该类位于sun.nio.cs包下面,其源码在我们jdk中是没有的。源码地址:http://www.docjar.com/html/api/sun/nio/cs/StreamEncoder.java.html。在此,ctrl+v其源码,如下:
[java]
/*
* Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package sun.nio.cs;
import java.io;
import java.nio;
import java.nio.channels;
import java.nio.charset;
public class StreamEncoder extends Writer
{
private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192;
private volatile boolean isOpen = true;
private void ensureOpen() throws IOException {
if (!isOpen)
throw new IOException("Stream closed");
}
// Factories for java.io.OutputStreamWriter
public static StreamEncoder forOutputStreamWriter(OutputStream out,
Object lo