zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    [20170612]FOR ALL COLUMNS SIZE repeat(11g).txt
    [20170612]FOR ALL COLUMNS SIZE repeat(12c).txt
    [20170611]关于数据块地址的计算.txt
    [20170607]再论Private Strand Flush Not Complete.txt
    [20170606]11G _optimizer_null_aware_antijoin.txt
    42_自定义泛型类的应用
    43_通过反射获得泛型的实际类型参数
    为什么好男孩找不到女朋友
    38_泛型的通配符扩展应用
    36_入门泛型的基本应用
  • 原文地址:https://www.cnblogs.com/fujilong/p/4703482.html
Copyright © 2011-2022 走看看