java io读书笔记(7) Closing Output Streams
输出完毕后,需要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的更多相关文章
- java io读书笔记(5) Writing Bytes to Output Streams
outputstream类是所有的字符输出类的父类,他是一个抽象类. 对于OutputStream类来说,其最基础的方法就是:write(). public abstract void write(i ...
- java io读书笔记(2)什么是stream
什么是stream?stream就是一个长度不确定的有序字节序列. Input streams move bytes of data into a Java program from some gen ...
- java io读书笔记(1)综述
学习,是要持之以恒的,再读一本书,坚持. Java™ I/O, 2nd Edition By Elliotte Rusty Harold ............................... ...
- java io读书笔记(6) Writing Arrays of Bytes
显而易见,一次性写出一堆数据,要比一个byte一个byte的写,快多了,因此,outputstream,给出了2个增强型的write: public void write(byte[] data) t ...
- java io读书笔记(8)FileInputStream/FileOutputStream的应用
转自:http://www.cnblogs.com/jjtech/archive/2011/04/17/2019210.html 这是一对继承于InputStream和OutputStream的类,用 ...
- java io读书笔记(3)数值类型的数据
input stream读取字节:out stream写入字节.Readers读取字符而Writers写入字符.因此,如果我们想理解input和output,我们首先就要明白 java如何处理字节,整 ...
- java io读书笔记(4)字符数据
Number只是java程序中需要读出和写入的一种数据类型.很多java程序需要处理有一大堆的字符组成的text,因为计算机真正懂得的只有数字,因此,字符按照某种编码规则,和数字对应. 比如:在ASC ...
- java effective 读书笔记
java effective 读书笔记 []创建和销毁对象 静态工厂方法 就是“封装了底层 暴露出一个访问接口 ” 门面模式 多参数时 用构建器,就是用个内部类 再让内部类提供构造好的对象 枚举 si ...
- Java IO学习笔记四:Socket基础
作者:Grey 原文地址:Java IO学习笔记四:Socket基础 准备两个Linux实例(安装好jdk1.8),我准备的两个实例的ip地址分别为: io1实例:192.168.205.138 io ...
随机推荐
- JavaScript 数组详解(转)
在程序语言中数组的重要性不言而喻,JavaScript中数组也是最常使用的对象之一,数组是值的有序集合,由于弱类型的原因,JavaScript中数组十分灵活.强大,不像是Java等强类型高级语言数组只 ...
- mysql安装tcmalloc
TCMalloc(Thread-Caching Malloc)是google-perftools工具中的一个,与标准的glibc库的malloc相比,TCMalloc在内存的分配上效率和速度要高得多, ...
- cursor:pointer
.cursor_hand{ cursor:pointer; }
- THE ARCHITECTURE OF COMPLEXITY HERBERT A. SIMON* Professor of Administration, Carnegie Institute of Technology (Read April 26, 1962)
THE ARCHITECTURE OF COMPLEXITY HERBERT A. SIMON* Professor of Administration, Carnegie Institute of ...
- P1003 铺地毯
水题 #include <bits/stdc++.h> using namespace std; const int maxn = 10005; int n; int x, y, i; s ...
- 初学MyBatis.net
1.MyBatis.net介绍 MyBatis..net是一个简单,但是完整的ORM框架,它使你的实体对象与sql语句或者存储过程之间的映射变得很简单,并提供数据访问.包括两个主要框架 DataAcc ...
- session和cookie区别
<?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- jira attachement directorey,workflow---extention.
workflow---extention. https://confluence.atlassian.com/jirakb/jira-miscellaneous-workflow-extensions ...
- 使用dotTrace6.0进行内存分析
dotTrace6.0提供了内存分析功能,统计抓取的时间段内各个堆栈执行过程中使用的内存大小,按照堆栈执行情况树状排序:和它之前提供的时间统计类似,粗截了几个页面,希望对大家有所帮助. 下载安装Jet ...
- 【C51】单片机芯片之——图解74HC595
第一部部分用于快速查阅使用,详细的使用见文章第二部分 引脚图