java 编程规范解读
1. try catch
finally 块必须对资源对象、流对象进行关闭,有异常也要做 try-catch。
说明:如果 JDK7 及以上,可以使用 try-with-resources 方式。
2. JDK7 特性之 try-with-resource 资源的自动管理
该try-with资源语句是try声明了一个或多个资源声明。一个资源是程序与它完成后,必须关闭的对象。该try-with资源语句确保每个资源在发言结束时关闭。
任何实现的java.lang.AutoCloseable对象(包括实现的所有对象)java.io.Closeable都可以用作资源。
@Test
public void test2() throws IOException {
String filepath = "D:\\gui-config.json";
try (
FileReader fileReader = new FileReader(filepath);
BufferedReader br = new BufferedReader(fileReader)
) {
String curline = null;
while ((curline = br.readLine()) != null) {
System.out.println(curline);
}
}
}
// FileReader 和 BufferedReader 均实现了 AutoCloseable 接口
FileReader Closeable
// extends InputStreamReader
public class FileReader extends InputStreamReader
// InputStreamReader extends Reader
public class InputStreamReader extends Reader
// Reader implements Readable, Closeable
public abstract class Reader implements Readable, Closeable
BufferReader
// extends Reader
public class BufferedReader extends Reader
// Reader implements Readable, Closeable
public abstract class Reader implements Readable, Closeable
3. 自定义 AutoCloseable 实现类
3.1 自定义自动关闭类AutoCloseDemo
package core.java.autoclosable;
/**
* @ClassName AutoCloseTest
* @description: 自定义自动关闭类;
* @author: peiwisdom
* @create: 2020-09-10 17:49
**/
public class AutoCloseDemo implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("关闭");
}
public void run(boolean flag) {
if (flag) {
System.out.println("业务处理");
} else {
System.out.println("出现异常");
throw new RuntimeException("自定义RuntimeException");
}
}
}
3.2 测试类
package core.java.autoclosable;
import org.junit.Test;
/**
* @ClassName AutoCloseDemoTest
* @description: 自动关闭测试
* @author: peiwisdom
* @create: 2020-09-10 17:54
**/
public class AutoCloseDemoTest {
@Test
public void test3() throws Exception {
try (AutoCloseDemo autoCloseTestModel = new AutoCloseDemo()) {
autoCloseTestModel.run(false);
}
}
}
3.3 执行结果
"C:\Program Files\Java\jdk1.8.0_251\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=58651:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit5-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\rt.jar;D:\open-source\graphresolvedesignpartner\target\test-classes;D:\open-source\graphresolvedesignpartner\target\classes;C:\Users\peizihui\.m2\repository\junit\junit\4.10\junit-4.10.jar;C:\Users\peizihui\.m2\repository\org\hamcrest\hamcrest-core\1.1\hamcrest-core-1.1.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 core.java.autoclosable.AutoCloseDemoTest,test3
出现异常
关闭
java.lang.RuntimeException: 自定义RuntimeException
at core.java.autoclosable.AutoCloseDemo.run(AutoCloseDemo.java:21)
at core.java.autoclosable.AutoCloseDemoTest.test3(AutoCloseDemoTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Process finished with exit code -1
4. 参考
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
网友评论