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 ...
随机推荐
- sshd调优
sshd调优:禁用dns查找,加快速度在sshd_config中设置:UseDNS no禁用root登录:建立普通用户在sshd_config中设置PermitRootLogin no以上设置需要重启 ...
- asp.net webform杂记
context.Response.ContentType = "text/plain"; string encodeFileName = HttpUtilit ...
- laravel 部分路由取消csrf
// app/Http/Middleware/VerifyCsrfToken protected $except = [ 'webhook/*' ];
- coursera python 学习总结
为啥要写这篇总结?早上突然想到了四个字:知行合一.实践,总结,再实践,再总结经验,积累经验,为己所用.闲话少叙,来干货: 1.目标要单一,如果想要完成课程,还要健身,还要玩玩游戏.看看电影,还学别的课 ...
- Law of total probability
https://en.wikipedia.org/wiki/Law_of_total_probability the total probability of an outcome which can ...
- Delphi下的OpenGL开发入门
unit Unit1; interface uses OpenGL,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls ...
- X5学习笔记—给单元格添加颜色
设置grid某一个单元格的颜色 可以用dhtmlxgrid的原生态方法 setCellTextStyle (row_id, ind, styleString) 参数: rowid:行id cellin ...
- hadoop与云技术、云计算混肴澄清
本文引用自:http://www.aboutyun.com/blog-61-248.html 一.初学者问题: 请教个问题在实际的生成环境里面,数据源产生的地方部署Hadoop,还是需要程序把数据给迁 ...
- Weak Pair---hud5877大连网选(线段树优化+dfs)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 题意:给你一颗树,有n个节点,每个节点都有一个权值v[i]:现在求有多少对(u,v ...
- 利用HTML和JS制作隔行换背景颜色的表格
1.源代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...