1 流
序号 | 方法 | 描述 |
---|---|---|
1 | write | 写入数据 |
2 | flush | 刷新内存数据 |
3 | close | 关闭流 |
2 流关闭
FileOutputStream是接收数据的管道,使用结束,需要进行关闭,如果不进行关闭,可以一直向这个管道写入数据,会导致OOM,若出现异常,JVM不能正常回收该对象,内存泄露,可能最终导致OOM。
使用流之后,必须关闭
。
关闭流有两种方式:
- try…catch…finally在finally里统一关闭
- Java7及之后,在try(io)中自动关闭
I/O流源码介绍如下:
2.1 FileOutputStream
FileOutputStream是常用的数据接收通道,常用的方法有写(write)、读(read)、关闭(close),其中关闭方法继承OutputStream,因此FileOutputStream使用的close为OutputStream的方法,在close方法中调用了channel类,该类在AbstractInterruptibleChannel类中实现。
public
class FileOutputStream extends OutputStream
{
/**
* Closes this file output stream and releases any system resources
* associated with this stream. This file output stream may no longer
* be used for writing bytes.
*
* <p> If this stream has an associated channel then the channel is closed
* as well.
*
* @exception IOException if an I/O error occurs.
*
* @revised 1.4
* @spec JSR-51
*/
public void close() throws IOException {
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
}
if (channel != null) {
channel.close();
}
fd.closeAll(new Closeable() {
public void close() throws IOException {
close0();
}
});
}
}
2.2 OutputStream
OutputStream为抽象类,关闭方法源码如下:
package java.io;
public abstract class OutputStream implements Closeable, Flushable {
public abstract void write(int b) throws IOException;
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
public void write(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
for (int i = 0 ; i < len ; i++) {
write(b[off + i]);
}
}
public void flush() throws IOException {
}
public void close() throws IOException {
}
}
2.3 FileChannel
public abstract class FileChannel
extends AbstractInterruptibleChannel
implements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel
{
2.4 AbstractInterruptibleChannel
public abstract class AbstractInterruptibleChannel
implements Channel, InterruptibleChannel
{
public final void close() throws IOException {
synchronized (closeLock) {
if (!open)
return;
open = false;
implCloseChannel();
}
}
}
2.4 ObjectOutputStream
关闭close调用了flush和clear。
public class ObjectOutputStream
extends OutputStream implements ObjectOutput, ObjectStreamConstants
{
/**
* Closes the stream. This method must be called to release any resources
* associated with the stream.
*
* @throws IOException If an I/O error has occurred.
*/
public void close() throws IOException {
flush();
clear();
bout.close();
}
}
3 小结
- I/O用完必须关闭,如果不进行关闭,其一,可以一直向这个管道写入数据,会导致OOM,若出现异常,JVM不能正常回收该对象,内存泄露,可能最终导致OOM;其二,不关闭,并且未调用flush刷新内存方法,会丢失数据,缓存的空间为8192字节,若数据量不是8192字节的N倍,不会自动刷新,导致,小于8192字节的数据无法刷新到缓存中,丢失数据
- close()方法分别调用了flush和clear,即使用
- flush刷新内存,保证数据完全写入,clear释放内存
【参考文献】
[1]https://blog.csdn.net/u011541946/article/details/81150610
[2]https://blog.csdn.net/u012383839/article/details/88625262
[3]https://www.cnblogs.com/Cuimc/archive/2004/01/13/10755192.html