s-day09-JDK7异常的处理方式
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) {
|
||||
System.out.println("捕获了异常");
|
||||
}
|
||||
|
||||
System.out.println("程序结束");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.inmind.io_exception05;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/*
|
||||
JDK7异常的处理方式
|
||||
*/
|
||||
public class Jdk7Demo14 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
try(FileInputStream fis = new FileInputStream("3.jpg");
|
||||
FileOutputStream fos = new FileOutputStream("4.jpg")) {
|
||||
//文件复制(边读边写)
|
||||
byte[] bytes = new byte[1024];
|
||||
int len;
|
||||
|
||||
while ((len = fis.read(bytes)) != -1) {
|
||||
fos.write(bytes, 0, len);
|
||||
}
|
||||
}catch (IOException 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