黑马程序员技术交流社区
标题:
求大神帮忙。怎么报错??
[打印本页]
作者:
看好时机向前冲
时间:
2016-3-15 23:57
标题:
求大神帮忙。怎么报错??
public class ExceptionDemo{
public static void main(String[] args) {
File file = new File("c:\\a.txt");
readFile1(file);
}
public static void readFile1(File file) {
// 使用字节流读取文件的异常处理.
// 1. 创建字节输入流.
FileInputStream fis = null;
try {
// 1. 创建字节输入流.
fis = new FileInputStream(file);
// 2. 读取文件.
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
fis.close();
}
}
}
}
}
作者:
ameanboy
时间:
2016-3-16 12:38
楼主没有正确处理异常(没有抛出,也没有try catch解决),建议好好回顾一下IO和异常的两节课
public class ExceptionDemo {
public static void main(String[] args) throws IOException {
File file = new File("c:\\a.txt");
readFile1(file);
}
public static void readFile1(File file) throws IOException {
// 使用字节流读取文件的异常处理.
// 1. 创建字节输入流.
FileInputStream fis = null;
try {
// 1. 创建字节输入流.
fis = new FileInputStream(file);
// 2. 读取文件.
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
fis.close();
}
}
}
作者:
骓逝
时间:
2016-3-16 15:02
close方法会抛出一个异常,需要处理,还有小是{}看下有没有多
作者:
骓逝
时间:
2016-3-16 15:03
close前需要判断下fis是不是null
作者:
吃肉的小绵羊
时间:
2016-3-16 16:59
fis.close()会出现异常,你要判断一下,
if(fis!=null)
try
{ fis.close();
}
catch(IOException e)
{
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2