zoukankan      html  css  js  c++  java
  • IO异常 的处理

     1 package com.throwsss;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 
     9 /**
    10  *  IO异常 的处理      
    11  * @author Administrator
    12  *用 throw new RuntimeException(e);方法
    13  */
    14 class Read{
    15     public static void readTest(){
    16         File file = new File("D://aa.txt");
    17         InputStream inputStream = null;
    18         try {
    19             inputStream = new FileInputStream(file);
    20             byte[] bs = new byte[1024];
    21             int length = 0;
    22             while((length = inputStream.read(bs)) != -1){
    23                 String str = new String(bs,0,length);
    24                 System.out.println(str);
    25             }
    26         } catch (FileNotFoundException e) {
    27             //e.printStackTrace();
    28             throw new RuntimeException(e);
    29         } catch (IOException e){
    30             //e.printStackTrace();
    31             throw new RuntimeException(e);
    32         } finally{
    33             if(inputStream != null){
    34             try {
    35                 inputStream.close();
    36                 System.out.println("文件釋放成功");
    37             } catch (IOException e) {
    38                 // TODO Auto-generated catch block
    39                 //e.printStackTrace();
    40                 System.out.println("文件释放失败");
    41                 throw new RuntimeException(e);
    42             
    43             }
    44             }
    45         }
    46         
    47     }
    48 }
    49 public class Throwss {
    50 
    51     public static void main(String[] args) {
    52         // TODO Auto-generated method stub
    53 
    54         Read read = new Read();
    55         read.readTest();
    56     }
    57 
    58 }
  • 相关阅读:
    STM32:SPI&w25qxx的配置与代码
    STM32:USART的原理与配置
    C的抽象数据类型:二叉树
    DSP:TMS320C66x 系列SPI NOR自启动
    C的抽象数据类型:链表、队列
    STM32:GPIO口的使用
    STM32:时钟树
    STM32:预备知识
    makefile:简单小结
    ubuntu:tar、apt、vim、gcc的配置和简单使用
  • 原文地址:https://www.cnblogs.com/fujilong/p/4703403.html
Copyright © 2011-2022 走看看