使用IO流技术的时候,我们不能将所有异常都抛出去,不然以后开发项目到时候,异常都交给了使用者。
这里我们将所有异常都进行“内部消化”。
最后关流的时候要在finally中进行。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class IoDemo {
public static void main(String[] args) {
FileInputStream fi = null;
try {
fi = new FileInputStream("F:/lishuai.txt");
int len = 0;
byte[] b = new byte[1024];
while ((len = fi.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fi != null) {
try {
fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
网友评论