• IO异常 的处理 test


     1 package com.throwsss;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 import java.io.InputStream;
     9 
    10 class Picture{
    11     public static void readWrite(){
    12     File file = new File("D://abc.jpg");
    13     File file2 = new File("F://abc.jpg");
    14     InputStream inputStream = null;
    15     FileOutputStream fileOutputStream = null;
    16     try {
    17         inputStream = new FileInputStream(file);
    18         fileOutputStream = new FileOutputStream(file2);
    19         byte[] bs = new byte[1024];
    20         int length = 0;
    21         try {
    22             while((length = inputStream.read(bs))!=-1){
    23                 fileOutputStream.write(bs, 0, length);
    24             }
    25         } catch (IOException e) {
    26             // TODO Auto-generated catch block
    27             throw new RuntimeException(e);
    28           }
    29         } catch (FileNotFoundException e) {
    30         // TODO Auto-generated catch block
    31           throw new RuntimeException(e);
    32         }finally{
    33             if(fileOutputStream != null){
    34                 try {
    35                     fileOutputStream.close();
    36                     System.out.println("关闭输出流资源成功...");
    37                 } catch (IOException e) {
    38                     System.out.println("关闭输出流资源失败...");
    39                     throw new RuntimeException(e);
    40                 }finally{
    41                     if(inputStream != null){
    42                         try {
    43                             inputStream.close();
    44                             System.out.println("关闭输入流对象成功...");
    45                         } catch (IOException e) {
    46                             System.out.println("关闭输入流对象失敗...");
    47                             throw new RuntimeException(e);
    48                         }
    49                     }
    50                 }
    51             }
    52         }
    53     
    54     } 
    55 }
    56 
    57 public class Throwtest {
    58 
    59     public static void main(String[] args) {
    60         // TODO Auto-generated method stub
    61 
    62         Picture picture = new Picture();
    63         picture.readWrite();
    64     }
    65 
    66 }
  • 相关阅读:
    _DataStructure_C_Impl:图的邻接矩阵存储
    ios的单元測试OCUnit以及更新了之后的XCTestCase
    java之 ------ 可变參数和卫条件
    【能力提升】SQL Server常见问题介绍及高速解决建议
    NYOJ 116 士兵杀敌 (线段树,区间和)
    Spring和MyBatis环境整合
    TypeScript和JavaScript的一些小技巧记录
    VSCode配置TypeScript
    function 与 => 的区别
    Egret里用矢量挖圆形的洞
  • 原文地址:https://www.cnblogs.com/fujilong/p/4703482.html
Copyright © 2020-2023  润新知