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();
}
}
}
}
}
|
|