io之异常处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.Lucky.io;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
/*
   io异常的处理方法:
 */
public class ioTryCatch {
    public static void main(String[] args) throws IOException {
 
        System.out.println("----- //初级使用try catch-------");
 
        FileInputStream inputTry=null;   //必须先赋值为null
        FileOutputStream outTry=null;
        try {
            //创建
            inputTry=new FileInputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\test.txt");   //读取对象
            outTry=new FileOutputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\copymaxData.txt");  //输出对象
 
            //创建一次读取的字节数组
            byte[] arrTry=new byte[5];   //数组长度为5
 
            //遍历
            int valTry;  //每一次遍历几个字节
 
            while ((valTry=inputTry.read(arrTry))!=-1){  //读取
                //输出到copy文件中【参数1:要写的数据源;参数2:开始索引;参数3:个数】
                outTry.write(arrTry,0,valTry);
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源{先开后关}  --》可以保证数据读取完整
            //判断outTry对象是否是null
            if(outTry!=null){
                try {//因为close可能也存在异常
                    outTry.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
            //判断inputTry对象是否是null
            if(outTry!=null){
                try {//因为close可能也存在异常
                    inputTry.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
 
 
        System.out.println();
        System.out.println("----- //中级级使用try catch【jdk7的写法】-------");
 
        try (FileInputStream iTry=new FileInputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\test.txt");   //读取对象
             FileOutputStream  oTry=new FileOutputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\copymaxData.txt");  ){
 
            //创建一次读取的字节数组
            byte[] arrTry=new byte[5];   //数组长度为5
            //遍历
            int valTry;  //每一次遍历几个字节
 
            while ((valTry=iTry.read(arrTry))!=-1){  //读取
                //输出到copy文件中【参数1:要写的数据源;参数2:开始索引;参数3:个数】
                oTry.write(arrTry,0,valTry);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
        ////////////////////////////////////////////////////////////////////////////////////////
        System.out.println();
        System.out.println("----- //中级级使用try catch【jdk9的写法】-------");
        FileInputStream fis=new FileInputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\test.txt");
        FileOutputStream  fos=new FileOutputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\copymaxData.txt");
        try (fis;fos){
            //创建一次读取的字节数组
            byte[] arrTry=new byte[5];   //数组长度为5
            //遍历
            int valTry;  //每一次遍历几个字节
 
            while ((valTry=fis.read(arrTry))!=-1){  //读取
                //输出到copy文件中【参数1:要写的数据源;参数2:开始索引;参数3:个数】
                fos.write(arrTry,0,valTry);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
 
    }
}

  

posted @   唯易人生  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示