java 输入输出IO流 IO异常处理try(IO流定义){IO流使用}catch(异常){处理异常}finally{死了都要干}

IO异常处理

之前我们写代码的时候都是直接抛出异常,但是我们试想一下,如果我们打开了一个流,在关闭之前程序抛出了异常,那我们还怎么关闭呢?这个时候我们就要用到异常处理了。
try-with-resource语句: 确保在异常出现后 打开的流能自动关闭,无需单独再写.close()流关闭语句。
语法:
try(构建流通道语句){
  业务处理逻辑
}catch(异常){
  异常处理逻辑
}finally{
  ....
}
 
示例代码:
复制代码
import java.io.*;
import java.nio.charset.Charset;

/**
 * @ClassName FileCopyTryCatchExample
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/17.
 */
public class FileCopyTryCatchExample {
    public static void main(String[] args){
        String srcPath = "C:\\Users\\ZHANGMINGDA\\Pictures\\康熙北巡.jpg";
        String dstpath = "C:\\Users\\ZHANGMINGDA\\Pictures\\康熙北巡bak.jpg";
        char[] tmpchars = new char[1024];
        int readLength;

//        try(Reader fileReader = new FileReader(srcPath); Writer fileWriter = new FileWriter(dstpath)){
        try(Reader fileReader = new MyFileReader(srcPath); Writer fileWriter = new MyFileWriter(dstpath)){
            while ((readLength = fileReader.read(tmpchars)) != -1){
                fileWriter.write(tmpchars,0,readLength);
            }
//            fileReader.close();
//            fileWriter.close();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            System.out.println("done");
        }
    }
//测试try(构建流)语句是否自动关闭流,重写FileReader和FileWriter public static class MyFileReader extends FileReader{ public MyFileReader(String fileName) throws FileNotFoundException { super(fileName); } public MyFileReader(File file) throws FileNotFoundException { super(file); } public MyFileReader(FileDescriptor fd) { super(fd); } public MyFileReader(String fileName, Charset charset) throws IOException { super(fileName, charset); } public MyFileReader(File file, Charset charset) throws IOException { super(file, charset); } @Override public void close() throws IOException { super.close(); System.out.println("关闭了输入流"); } } public static class MyFileWriter extends FileWriter { public MyFileWriter(String fileName) throws IOException { super(fileName); } public MyFileWriter(String fileName, boolean append) throws IOException { super(fileName, append); } public MyFileWriter(File file) throws IOException { super(file); } public MyFileWriter(File file, boolean append) throws IOException { super(file, append); } public MyFileWriter(FileDescriptor fd) { super(fd); } public MyFileWriter(String fileName, Charset charset) throws IOException { super(fileName, charset); } public MyFileWriter(String fileName, Charset charset, boolean append) throws IOException { super(fileName, charset, append); } public MyFileWriter(File file, Charset charset) throws IOException { super(file, charset); } public MyFileWriter(File file, Charset charset, boolean append) throws IOException { super(file, charset, append); } @Override public void close() throws IOException { super.close(); System.out.println("关闭了输出流"); } } }
复制代码

 

 

posted on   zhangmingda  阅读(602)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示