首页 > 其他分享 >利用try-catch-finally处理异常方式

利用try-catch-finally处理异常方式

时间:2022-11-15 10:15:36浏览次数:38  
标签:fr fw read try finally catch new

package com.msb.io01;

import java.io.*;

/**
 * @author : liu
 * 日期:08:11:44
 * 描述:IntelliJ IDEA
 * 版本:1.0
 */
public class Test04 {
    //这是一个main方法:是程序的入口
    public static void main(String[] args)  {
        //1.有一个源文件
        File f1=new File("d:\\lol.jpg");
        //2.有一个目标文件
        File f2=new File("d:\\Demo.jpg");
        //3.输入对象
        FileReader fr= null;
        //4.输出对象
        FileWriter fw= null;
        try {
            fr = new FileReader(f1);
            try {
                fw = new FileWriter(f2,true);
                //5.开始复制操作
                char[] ch=new char[5];
                int read = fr.read(ch);
                while (read!=-1){
                    //先转换字符串也可以
                    String st=  new  String(ch,0,read);
                    fw.write(st);
                    read=fr.read(ch);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }finally {

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            //6.关闭流
            try {
          //方式空指针异常 if (fw!=null){ fw.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (fr!=null){ fw.close(); } } catch (IOException e) { e.printStackTrace(); } } } }

 

标签:fr,fw,read,try,finally,catch,new
From: https://www.cnblogs.com/jeldp/p/16891471.html

相关文章