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("关闭了输出流"); } } }
分类:
Java
posted on 2021-04-17 22:09 zhangmingda 阅读(602) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· 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工具