s-day09--JDK7IO异常的处理方式(重点)
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
package com.inmind.io_exception05;
|
||||||
|
|
||||||
|
public class Demo15 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try (Student s = new Student()){
|
||||||
|
System.out.println("try中代码执行完毕");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.inmind.io_exception05;
|
||||||
|
|
||||||
|
import java.io.FileWriter;
|
||||||
|
//17.JDK7异常的处理方式
|
||||||
|
public class JDK7_IO_Demo14 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//JDK7的语法糖,它会主动调用try()中流对象的close方法
|
||||||
|
try (FileWriter fw = new FileWriter("target.txt")) {
|
||||||
|
fw.write("这是jdk7的IO异常处理");
|
||||||
|
fw.flush();
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.io_exception05;
|
||||||
|
|
||||||
|
public class Student implements AutoCloseable{
|
||||||
|
@Override
|
||||||
|
public void close() throws Exception {
|
||||||
|
System.out.println("学生类的close方法执行了");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user