JDK7中引入了哪些与Java异常处理相关的更改?
自从引入Java7try-with资源以来。在这种情况下,我们在try块中声明一个或多个资源,这些资源在使用后将自动关闭。(在try块的末尾)
我们在try块中声明的资源应扩展java.lang.AutoCloseable类。
示例
以下程序演示了Java中的try-with-resources。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopying {
public static void main(String[] args) {
try(FileInputStream inS = new FileInputStream(new File("E:\\Test\\sample.txt"));
FileOutputStream outS = new FileOutputStream(new File("E:\\Test\\duplicate.txt"))){
byte[] buffer = new byte[1024];
int length;
while ((length = inS.read(buffer)) > 0) {
outS.write(buffer, 0, length);
}
System.out.println("File copied successfully!!");
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}输出结果
File copied successfully!!
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短