📄 fileioexceptionsample2.java
字号:
//FileIOExceptionSample2.java
import java.io.*;
public class FileIOExceptionSample2{
public static void main(String args[]) {
FileInputStream reader = null;
try {
int data;
reader = new FileInputStream("sample.txt");
System.out.println("\nreading from file sample.txt...");
while ( (data = reader.read()) != -1) {
System.out.print( (char) data);
}
}
catch (FileNotFoundException e) {
System.out.println("\n***FileNotFoundException***\n");
System.out.println("---Method printStackTrace information---");
e.printStackTrace();
System.out.println("---Method toString information---");
System.out.println(e.toString());
}
catch (IOException e1) {
System.out.println("\n***IOException when reading file***\n");
System.out.println("---Method printStackTrace information---");
e1.printStackTrace();
System.out.println("---Method toString information---");
System.out.println(e1.toString());
}
finally {
if (reader != null) {
try {
System.out.println("\nClosing file sample.txt...");
reader.close();
}
catch (IOException e2) {
System.out.println("\n***IOException when closing file***\n");
System.out.println("---Method printStackTrace information---");
e2.printStackTrace();
System.out.println("---Method toString information---");
System.out.println(e2.toString());
}
}
else
System.out.println("\nThe file sample.txt has not been openned!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -