Java字节流的操作

1、字节流的操作:以输入流为例

File file=new File("E:\\java\\上机项目.txt");
FileInputStream fis=null;
try {
fis=new FileInputStream(file);
byte b[]=new byte[1024];
int len=-1;//len如果到文件尾巴的话-1,没到文件尾表示读入到字节数组中的长度
while((len=fis.read(b))!=-1){
String str=new String(b,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

2、Throwable是所有异常的超类,有两个子类Error和Exception

    Error:系统崩溃,内存溢出,方法栈溢出(我们自己写的程序控制不了的错)
    Exception:
        1)需要强制使用try{}catch(){}的异常叫做强制检测异常(Checked Exception):FileNotFoundException,IOException

        注意:异常被捕获以后,try{}catch(){} 后面的代码依然能正常执行


        2)运行时异常(RuntimeException):数组下标越界,NullPointerException
        注意:发生运行时异常,那么程序立即终止
3,异常的监视和捕获是一个单独的线程

猜你喜欢

转载自blog.csdn.net/littlemangoyx/article/details/78113617