黑马程序员技术交流社区
标题:
JDK1.6及前,和JDK1.7,IO流异常处理标准代码对比
[打印本页]
作者:
LARK
时间:
2016-10-19 19:21
标题:
JDK1.6及前,和JDK1.7,IO流异常处理标准代码对比
JDK1.6及之前,IO流异常处理标准代码:
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("yyy.txt");
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}finally {
try{
if(fis != null)
fis.close();
}finally { //try fianlly的嵌套目的是能关一个尽量关一个
if(fos != null)
fos.close();
}
}
}
JDK1.7,IO流异常处理标准代码:
public static void main(String[] args) throws IOException {
try
(
FileInputStream fis = new FileInputStream("xxx.txt");
//try后小括号内的implements AutoClosable
FileOutputStream fos = new FileOutputStream("yyy.txt");
MyClose mc = new MyClose();
)
{
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
//不用手动关流
}
}
作者:
Kevin_Ye
时间:
2016-10-20 00:07
谢谢分享
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2