输出完毕后,需要close这个stream,从而使操作系统释放相关的资源。举例:

public void close( ) throws IOException

并不是所有的stream都需要close,可是,诸如file或者network,打开后,需要关闭。

try {
OutputStream out = new FileOutputStream("numbers.dat");
// Write to the stream...
out.close( );
}
catch (IOException ex) {
System.err.println(ex);
}

However, this code fragment has a potential leak. If an IOException is thrown while writing, the stream won't be closed. It's more reliable to close the stream in a finally block so that it's closed whether or not an exception is thrown. To do this you need to declare the OutputStream variable outside the try block. For example:

// Initialize this to null to keep the compiler from complaining
// about uninitialized variables
OutputStream out = null;
try {
out = new FileOutputStream("numbers.dat");
// Write to the stream...
}
catch (IOException ex) {
System.err.println(ex);
}
finally {
if (out != null) {
try {
out.close( );
}
catch (IOException ex) {
System.err.println(ex);
}
}
}

java io读书笔记(7) Closing Output Streams的更多相关文章

  1. java io读书笔记(5) Writing Bytes to Output Streams

    outputstream类是所有的字符输出类的父类,他是一个抽象类. 对于OutputStream类来说,其最基础的方法就是:write(). public abstract void write(i ...

  2. java io读书笔记(2)什么是stream

    什么是stream?stream就是一个长度不确定的有序字节序列. Input streams move bytes of data into a Java program from some gen ...

  3. java io读书笔记(1)综述

    学习,是要持之以恒的,再读一本书,坚持. Java™ I/O, 2nd Edition By Elliotte Rusty Harold ............................... ...

  4. java io读书笔记(6) Writing Arrays of Bytes

    显而易见,一次性写出一堆数据,要比一个byte一个byte的写,快多了,因此,outputstream,给出了2个增强型的write: public void write(byte[] data) t ...

  5. java io读书笔记(8)FileInputStream/FileOutputStream的应用

    转自:http://www.cnblogs.com/jjtech/archive/2011/04/17/2019210.html 这是一对继承于InputStream和OutputStream的类,用 ...

  6. java io读书笔记(3)数值类型的数据

    input stream读取字节:out stream写入字节.Readers读取字符而Writers写入字符.因此,如果我们想理解input和output,我们首先就要明白 java如何处理字节,整 ...

  7. java io读书笔记(4)字符数据

    Number只是java程序中需要读出和写入的一种数据类型.很多java程序需要处理有一大堆的字符组成的text,因为计算机真正懂得的只有数字,因此,字符按照某种编码规则,和数字对应. 比如:在ASC ...

  8. java effective 读书笔记

    java effective 读书笔记 []创建和销毁对象 静态工厂方法 就是“封装了底层 暴露出一个访问接口 ” 门面模式 多参数时 用构建器,就是用个内部类 再让内部类提供构造好的对象 枚举 si ...

  9. Java IO学习笔记四:Socket基础

    作者:Grey 原文地址:Java IO学习笔记四:Socket基础 准备两个Linux实例(安装好jdk1.8),我准备的两个实例的ip地址分别为: io1实例:192.168.205.138 io ...

随机推荐

  1. 20145235 《Java程序设计》第4周学习总结

    代码托管截图 教材学习内容总结 继承 •继承:继承基本上就是避免多个类间重复定义共同行为. 我理解的就是:在编写程序的过程中可能会出现部分代码重复的现象,把重复的部分单独定义为一类(父类),在其他代码 ...

  2. a read only variable

    SHOW VARIABLES LIKE '%FORMAT%' SET DATE_FORMAT ='%Y-%m-%d' [SQL]SET DATE_FORMAT ='%Y-%m-%d' [Err] 12 ...

  3. PureBasic—数控编辑框与调节块和进度条

    三个有关上下限问题的控件,它们也是主要控件的组成部分,分别为:SpinGadget()        数控编辑框TrackBarGadget()    调节块控件ProgressBarGadget() ...

  4. Linux上free命令的输出

    一.明确概念 A buffer is something that has yet to be "written" to disk.  A cache is something t ...

  5. C++ 简易时间类

    .h file #ifndef LIBFRAME_DATETIME_H_ #define LIBFRAME_DATETIME_H_ #include <stdint.h> #include ...

  6. node.js的exprots工厂模式

    工厂类: /** * Created by zzq on 2015/5/15. */ module.exports = function(){ this.getProduct = function() ...

  7. 对象(List<T>)的序列化和反序列化

    本文描述将对象(List<T>)序列化到 XML 文档中和从 XML 文档中反序列化为对象(List<T>). 命名空间: System.Xml.Serialization 程 ...

  8. [LeetCode]题解(python):048-Rotate Image

    题目来源 https://leetcode.com/problems/rotate-image/ You are given an n x n 2D matrix representing an im ...

  9. appium testcase1(Java)

    官网源码地址 https://github.com/appium/sample-code/blob/47fc0305396b8322b727820ca55a07607395040c/sample-co ...

  10. sql server 2008查询窗口怎么显示行数

    工具->选项