Java笔记 #02# 带资源的try语句
索引
/
test.txt
待读取的内容
hello.
/
普通的 try.java
读取 test.txt 内容
package example; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; public class GeneralTry {
public static void main(String[] args) {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream("d:/labs/test.txt");
System.out.println((char) inputStream.read()); // 输出读到第一个字符,至于会不会与txt文本内容对应还与txt本身的字符编码相关。
} catch (FileNotFoundException e) { // 捕获异常是强制性的,对应 new FileInputStream...
e.printStackTrace();
} catch (IOException e) { // 捕获异常是强制性的,对应 inputStream.read ..
e.printStackTrace();
} finally { // 关闭资源是非强制性的,但是我们应该总是这么做
try {
inputStream.close();
if (inputStream != null) { // inputStream 有可能为空,为了防止出现空指针而导致程序gg ..
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("如果在输出窗看到这句话,说明程序没有gg");
}
} /*
output=
h
如果在输出窗看到这句话,说明程序没有gg
*/
/
带资源的 try.java
同样是读取 test.txt 内容
package example; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; public class ResourceTry {
public static void main(String[] args) {
try (FileInputStream inputStream = new FileInputStream("d:/labs/test.txt")) {
System.out.println((char) inputStream.read()); // 输出读到第一个字符,至于会不会与txt文本内容对应还与txt本身的字符编码相关。
} catch (FileNotFoundException e) { // 捕获异常仍然是强制的
e.printStackTrace();
} catch (IOException e) { // 捕获异常仍然是强制的
e.printStackTrace();
} // try 块退出时,会自动调用 inputStream.close()
System.out.println("如果在输出窗看到这句话,说明程序没有gg");
}
} /*
output=
h
如果在输出窗看到这句话,说明程序没有gg
*/
/
上述程序(带资源的 try程序)是在正常情况下(test.txt 文件存在)运行的,那么倘若 test.txt 不存在呢?尝试把 test.txt 改成一个不存在的 test2.txt 运行带资源的 try 测试程序输出结果如下:
/*
output=
如果在输出窗看到这句话,说明程序没有gg
java.io.FileNotFoundException: d:\labs\test2.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at example.ResourceTry.main(ResourceTry.java:10)
*/
如果第一个程序(普通 try)没有在 inputStream.close() 之前进行非空检查,程序将会因为 java.lang.NullPointerException 而中止(也就是gg)。
就像上面看到的,带资源的 try 测试程序同样可以正常向下执行,所以,带资源的 try 在调用 close() 前是有进行非空判断的,这样就确保了程序正常执行而不抛出 NullPointerException,需要注意的是,除了空指针异常不会发生, close() 抛出的其它异常需要另当别论!
/
可以参考的文档与资料:
Try-With Resource when AutoCloseable is null
Possible null pointer exception on autocloseable idiom
The try-with-resources Statement - java tutorials - oracle
Java language specification - 14.20.3
oracle blog - Project Coin:try-with-resources on a null resource
再补充几个:
Exception coming out of close() in try-with-resource
Is it important to add AutoCloseable in java?
How should I use try-with-resources with JDBC?
最新文章
- lua的table表处理 及注意事项
- ios实现屏幕旋转的方法
- Cobar-Client 实现策略总结
- Log4Cplus的介绍
- esriSRProjCS3Type Constants
- 用 alias 给常用命令取个别名
- 全球在一个 level 上思考的价值观和想法是一样的(转)
- mybatis代理类Demo
- Asp,NET控制文件上传的大小
- Text-查找文本
- SQL Server 跨服务器操作
- eval()和JSON.parse()的区别
- ACM-ICPC 2018 南京赛区网络预赛 E题
- cpp 区块链模拟示例(三)新基本原形工程的建立
- CMake--Set用法
- Java SE学习【一】
- java sendmail
- C#自定义事件 范例:监视变量变化
- openstack 部署笔记--keystone
- 20155216 2016-2017-2 《Java程序设计》第六周学习总结
热门文章
- linux 系统信息查看
- 【转】“菜”鸟理解.NET Framework(CLI,CLS,CTS,CLR,FCL,BCL)
- kafka window环境搭建
- H-数学考试 想法题+最新头文件 2018年长沙理工大学第十三届程序设计竞赛
- Dividing the Path POJ - 2373 dp
- 双节点weblogic集群安装
- HDFS架构详解-非官档
- FW 编译Android系统源码和内核源码
- iOS多线程编程之线程间的通信(转载)
- 网站优化不等于搜索引擎优化SEO