1、jdk7之前的关闭流的操作
/**
* jdk7以前关闭流的方式
*
* @author fcant
* */
public class CloseResourceBeforeJava7 {
private static final String FileName = "file.txt";
public static void main(String[] args) throws IOException {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(FileName);
char c1 = (char) inputStream.read();
System.out.println("c1=" + c1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
2、jdk7之后try-with-resource关闭资源
实现了 **java.lang.AutoCloseable**
接口(其中,它包括实现了 **java.io.Closeable**
的所有对象),可以使用作为资源。实现java.lang.AutoCloseable接口的Resource类:
/**
* 资源类
*
* @author fcant
* */
public class Resource implements AutoCloseable {
public void sayHello() {
System.out.println("hello");
}
@Override
public void close() throws Exception {
System.out.println("Resource is closed");
}
}
/**
* jdk7及以后关闭流的方式
*
* @author fcant
* */
public class CloseResourceAfterJava7 {
public static void main(String[] args) {
try(Resource resource = new Resource()) {
resource.sayHello();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
hello
Resource is closed
当存在多个打开资源的时候:
/**
* 资源2
*
* @author fcant
* */
public class Resource2 implements AutoCloseable {
public void sayhello() {
System.out.println("Resource say hello");
}
@Override
public void close() throws Exception {
System.out.println("Resource2 is closed");
}
}
/**
* jdk7及以后关闭流的方式
*
* @author fcant
* */
public class CloseResourceAfterJava7 {
public static void main(String[] args) {
try(Resource resource = new Resource(); Resource2 resource2 = new Resource2()) {
resource.sayHello();
resource2.sayhello();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
hello
Resource say hello
Resource2 is closed
Resource is closed
3、关于异常屏蔽
A.Java7之前-不使用catch抛出异常的写法
将上面的测试类进行修改使用方法抽取异常处理不使用catch抛出异常
/**
* CloseResourceAfterJava7
* <p>
* encoding:UTF-8
*
* @author Fcant 下午 22:04 2020/6/26/0026
*/
public class CloseResourceAfterJava7 {
public static void main(String[] args) {
try {
errorTest();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void errorTest() throws Exception {
Resource resource = null;
try {
resource = new Resource();
resource.sayHello();
}
finally {
if (resource != null) {
resource.close();
}
}
}
}
B.Java7之前-使用catch抛出异常的写法
/**
* CloseResourceAfterJava7
* <p>
* encoding:UTF-8
*
* @author Fcant 下午 22:04 2020/6/26/0026
*/
public class CloseResourceAfterJava7 {
public static void main(String[] args) {
try {
errorTest();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void errorTest() throws Exception {
Resource resource = null;
try {
resource = new Resource();
resource.sayHello();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (resource != null) {
resource.close();
}
}
}
}
C.Java7之后-显示被Suppressed屏蔽(未使用catch抛出)[即A中的问题]的异常
①不使用catch处理异常
/**
* CloseResourceAfterJava7
* <p>
* encoding:UTF-8
*
* @author Fcant 下午 22:04 2020/6/26/0026
*/
public class CloseResourceAfterJava7 {
public static void main(String[] args) {
try {
errorTest();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void errorTest() throws Exception {
try (Resource resource = new Resource();) {
resource.sayHello();
}
}
}
②使用catch处理异常与不使用catch一样
/**
* CloseResourceAfterJava7
* <p>
* encoding:UTF-8
*
* @author Fcant 下午 22:04 2020/6/26/0026
*/
public class CloseResourceAfterJava7 {
public static void main(String[] args) {
try {
errorTest();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void errorTest() throws Exception {
try (Resource resource = new Resource();) {
resource.sayHello();
} catch (Exception e) {
e.printStackTrace();
}
}
}
D.分析编译后的class文件查看AutoCloseable怎么处理屏蔽的异常
以下是测试类编译后的class源码文件,并非研究操作的功能源码
public class CloseResourceAfterJava7 {
public CloseResourceAfterJava7() {
}
public static void main(String[] args) {
try {
errorTest();
} catch (Exception var2) {
var2.printStackTrace();
}
}
private static void errorTest() throws Exception {
Resource resource = new Resource();
Throwable var1 = null;
try {
resource.sayHello();
} catch (Throwable var10) {
var1 = var10;
throw var10;
} finally {
if (resource != null) {
if (var1 != null) {
try {
resource.close();
} catch (Throwable var9) {
var1.addSuppressed(var9);
}
} else {
resource.close();
}
}
}
}
}
可以发现编译以后生成了try-catch-finally语句块 finally中的var1.addSuppressed(var9);
这里可以看到AutoCloseable底层已经转换了try catch,所以可以不用catch抛出异常。
另外如果写的不够优雅,IDEA也会提示进行转换的