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 }
  • 相关阅读:
    gtk在线文档
    spice remote-viewer 连接会话时自动重定向usb设备(记录)
    04、数组与Arrays工具类
    03、选择、循环结构
    02、基本概念
    01、初识Java
    0、计算机相关知识了解
    云服务器Centos7部署Tomcat服务器
    JavaSE基础(三)--Java基础语法
    JavaSE基础(二)--Java环境配置
  • 原文地址:https://www.cnblogs.com/fujilong/p/4703403.html
Copyright © 2011-2022 走看看