IO异常的处理

简介: IO异常处理

IO异常的处理

JDK7前处理

之前的入门练习,我们一直把异常抛出,而实际开发中并不能这样处理,建议使用try...catch...finally 代码块,处理异常部分,代码使用演示:

public class HandleException1 {
   
    public static void main(String[] args) {
   
          // 声明变量
        FileWriter fw = null;
        try {
   
            //创建流对象
            fw = new FileWriter("fw.txt");
            // 写出数据
            fw.write("黑马程序员"); //黑马程序员
        } catch (IOException e) {
   
            e.printStackTrace();
        } finally {
   
            try {
   
                if (fw != null) {
   
                    fw.close();
                }
            } catch (IOException e) {
   
                e.printStackTrace();
            }
        }
    }
}
AI 代码解读

JDK7的处理(扩展知识点了解内容)

还可以使用JDK7优化后的try-with-resource 语句,该语句确保了每个资源在语句结束时关闭。所谓的资源(resource)是指在程序完成后,必须关闭的对象。

格式:

try (创建流对象语句,如果多个,使用';'隔开) {
   
    // 读写数据
} catch (IOException e) {
   
    e.printStackTrace();
}
AI 代码解读

代码使用演示:

public class HandleException2 {
   
    public static void main(String[] args) {
   
          // 创建流对象
        try ( FileWriter fw = new FileWriter("fw.txt"); ) {
   
            // 写出数据
            fw.write("黑马程序员"); //黑马程序员
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }
}
AI 代码解读

JDK9的改进(扩展知识点了解内容)

JDK9中try-with-resource 的改进,对于引入对象的方式,支持的更加简洁。被引入的对象,同样可以自动关闭,无需手动close,我们来了解一下格式。

改进前格式:

// 被final修饰的对象
final Resource resource1 = new Resource("resource1");
// 普通对象
Resource resource2 = new Resource("resource2");
// 引入方式:创建新的变量保存
try (Resource r1 = resource1;
     Resource r2 = resource2) {
   
     // 使用对象
}
AI 代码解读

改进后格式:

// 被final修饰的对象
final Resource resource1 = new Resource("resource1");
// 普通对象
Resource resource2 = new Resource("resource2");

// 引入方式:直接引入
try (resource1; resource2) {
   
     // 使用对象
}
AI 代码解读

改进后,代码使用演示:

public class TryDemo {
   
    public static void main(String[] args) throws IOException {
   
           // 创建流对象
        final  FileReader fr  = new FileReader("in.txt");
        FileWriter fw = new FileWriter("out.txt");
           // 引入到try中
        try (fr; fw) {
   
              // 定义变量
            int b;
              // 读取数据
              while ((b = fr.read())!=-1) {
   
                // 写出数据
                fw.write(b);
              }
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }
}
AI 代码解读
目录
打赏
0
0
0
0
42
分享
相关文章
【Java异常】java.lang.ClassCastException: java.io.NotSerializableException cannot be cast to java.lang.S
【Java异常】java.lang.ClassCastException: java.io.NotSerializableException cannot be cast to java.lang.S
98 0
【YashanDB知识库】YashanDB JDBC驱动查询时抛出io fail:Read timed out异常
【YashanDB知识库】YashanDB JDBC驱动查询时抛出io fail:Read timed out异常
|
2月前
|
C#
【Azure Function】Function App出现System.IO.FileNotFoundException异常
Exception while executing function: xxxxxxx,The type initializer for 'xxxxxx.Storage.Adls2.StoreDataLakeGen2Reading' threw an exception. Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the
125 64
【YashanDB知识库】YashanDB JDBC驱动查询时抛出io fail:Read timed out异常
【YashanDB知识库】YashanDB JDBC驱动查询时抛出io fail:Read timed out异常
【Java面试题汇总】Java基础篇——String+集合+泛型+IO+异常+反射(2023版)
String常量池、String、StringBuffer、Stringbuilder有什么区别、List与Set的区别、ArrayList和LinkedList的区别、HashMap底层原理、ConcurrentHashMap、HashMap和Hashtable的区别、泛型擦除、ABA问题、IO多路复用、BIO、NIO、O、异常处理机制、反射
【Azure Spring Cloud】Java Spring Cloud 应用部署到Azure上后,发现大量的 java.lang.NullPointerException: null at io.lettuce.core.protocol.CommandHandler.writeSingleCommand(CommandHandler.java:426) at ... 异常
【Azure Spring Cloud】Java Spring Cloud 应用部署到Azure上后,发现大量的 java.lang.NullPointerException: null at io.lettuce.core.protocol.CommandHandler.writeSingleCommand(CommandHandler.java:426) at ... 异常
|
8月前
|
【Java】已解决java.io.ObjectStreamException异常
【Java】已解决java.io.ObjectStreamException异常
73 1
|
8月前
|
【Java】已解决java.io.UnsupportedEncodingException异常
【Java】已解决java.io.UnsupportedEncodingException异常
411 0
|
8月前
|
【Java】已解决java.io.InterruptedIOException异常
【Java】已解决java.io.InterruptedIOException异常
565 0
【Nginx异常】无法加载响应数据:No data found for resource with given identifier,后端服务报Caused by: java.io.IOExcepti
【Nginx异常】无法加载响应数据:No data found for resource with given identifier,后端服务报Caused by: java.io.IOExcepti
1512 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等