每日持续更新)jdk api之Closeable基础、应用、实战
博主18年的互联网软件开发经验,从一名程序员小白逐步成为了一名架构师,我想通过平台将经验分享给大家,因此博主每天会在各个大牛网站点赞量超高的博客等寻找该技术栈的资料结合自己的经验,晚上进行用心精简、整理、总结、定稿,每天都会整理到12点,为了就是能让大家能够真正了解该技术栈的真正原理,最终从程序员成为一名真正的架构师,写的不一定是全站做好的,但是是全站最用心的~。
以后我会推出一些列的文章,每天都会更新,每天进步一点点,发布顺序【java的api基础、应用、实战】->【java开源技术栈及源码分析】->【java开源技术栈整合】->【java低代码开发平台的建设】
一、java.io
1.12 Closeable
Closeable 接口是 Java 核心库中的一个接口,它定义了一个单一的方法 close(),用于关闭资源。Closeable 接口通常用于需要显式关闭的资源类,例如文件流、网络连接等。在 Java 中,实现了 Closeable 接口的类可以使用 try-with-resources 语句,确保在使用完资源后自动关闭。
Closeable 接口介绍:
-
包路径:
java.io -
继承关系: 接口
Closeable继承自AutoCloseable接口。
Closeable 接口所有字段:
Closeable 接口没有定义任何字段。
Closeable 接口构造方法:
Closeable 接口是一个接口,因此它没有构造方法。它被设计为被实现而不是直接实例化。
Closeable 接口方法摘要:
Closeable 接口只定义了一个方法:
-
void close() throws IOException:-
关闭资源的方法。实现类需要在这个方法中实现资源的关闭操作。抛出
IOException是为了兼容可能在关闭资源时发生的 I/O 异常。
-
简单使用例子:
下面是一个简单的示例,演示了如何实现 Closeable 接口并使用 try-with-resources 语句来自动关闭资源。
javaCopy code
import java.io.Closeable;
import java.io.IOException;
?
// 自定义实现 Closeable 接口的资源类
class MyResource implements Closeable {
?
? // 一些资源的状态和操作
?
? @Override
? public void close() throws IOException {
? ? ? // 实现关闭资源的逻辑
? ? ? System.out.println("Closing MyResource");
? }
}
?
public class CloseableExample {
?
? public static void main(String[] args) {
? ? ? // 使用 try-with-resources 语句创建 MyResource 对象
? ? ? try (MyResource resource = new MyResource()) {
? ? ? ? ? // 在这里使用 MyResource 对象进行一些操作
? ? ? ? ? System.out.println("Using MyResource");
? ? ? } catch (IOException e) {
? ? ? ? ? e.printStackTrace();
? ? ? }
? ? ? // 此时 MyResource 被自动关闭
? }
}
在这个例子中,MyResource 类实现了 Closeable 接口,并在 close 方法中定义了关闭资源的逻辑。在 main 方法中,使用了 try-with-resources 语句创建了一个 MyResource 对象,并在 try 块中使用该资源。当 try 块执行完毕时,无论是否发生异常,MyResource 都会被自动关闭。
应用场景
Closeable 接口通常用于需要手动关闭的资源类。以下是一些可能的应用场景,以及每个应用场景的简单代码实现示例。
-
文件读写操作
应用场景: 在进行文件读写操作时,通常需要打开文件流并在完成操作后关闭文件流。
代码实现:
javaCopy code
import java.io.*;
?
public class FileReadWriteExample {
?
? public static void main(String[] args) {
? ? ? // 使用 try-with-resources 语句打开文件流
? ? ? try (FileInputStream input = new FileInputStream("input.txt");
? ? ? ? ? ? FileOutputStream output = new FileOutputStream("output.txt")) {
?
? ? ? ? ? // 在这里进行文件读写操作
? ? ? ? ? int data;
? ? ? ? ? while ((data = input.read()) != -1) {
? ? ? ? ? ? ? output.write(data);
? ? ? ? ? }
?
? ? ? } catch (IOException e) {
? ? ? ? ? e.printStackTrace();
? ? ? }
? }
}
-
网络连接
应用场景: 在进行网络连接时,例如使用 Socket 进行通信,需要在完成通信后关闭 Socket 连接。
代码实现:
javaCopy code
import java.io.IOException;
import java.net.Socket;
?
public class NetworkCommunicationExample {
?
? public static void main(String[] args) {
? ? ? // 使用 try-with-resources 语句打开 Socket 连接
? ? ? try (Socket socket = new Socket("example.com", 8080)) {
? ? ? ? ? // 在这里进行网络通信操作
? ? ? ? ? // ...
? ? ? } catch (IOException e) {
? ? ? ? ? e.printStackTrace();
? ? ? }
? }
}
-
数据库连接
应用场景: 在进行数据库操作时,通常需要打开数据库连接并在完成数据库操作后关闭连接。
代码实现:
javaCopy code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
?
public class DatabaseOperationExample {
?
? public static void main(String[] args) {
? ? ? // 使用 try-with-resources 语句打开数据库连接
? ? ? try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password")) {
? ? ? ? ? // 在这里进行数据库操作
? ? ? ? ? // ...
? ? ? } catch (SQLException e) {
? ? ? ? ? e.printStackTrace();
? ? ? }
? }
}
-
自定义资源类
应用场景: 在自定义资源类时,实现 Closeable 接口以确保资源的正确关闭。
代码实现:
javaCopy code
import java.io.Closeable;
import java.io.IOException;
?
// 自定义资源类实现 Closeable 接口
class MyResource implements Closeable {
?
? @Override
? public void close() throws IOException {
? ? ? // 在这里实现关闭资源的逻辑
? ? ? System.out.println("Closing MyResource");
? }
}
?
public class CustomResourceExample {
?
? public static void main(String[] args) {
? ? ? // 使用 try-with-resources 语句创建自定义资源对象
? ? ? try (MyResource resource = new MyResource()) {
? ? ? ? ? // 在这里使用自定义资源对象进行一些操作
? ? ? ? ? System.out.println("Using MyResource");
? ? ? } catch (IOException e) {
? ? ? ? ? e.printStackTrace();
? ? ? }
? ? ? // 此时 MyResource 被自动关闭
? }
}
在每个示例中,使用了 try-with-resources 语句,确保资源在使用完毕后自动关闭。这有助于减少资源泄漏的可能性,并提高代码的可靠性。
实战例子
以下是一个实战例子,演示了如何使用 Closeable 接口和 try-with-resources 语句来处理文件复制操作。在这个例子中,我们将创建一个文件复制工具类,利用 FileInputStream 和 FileOutputStream 进行文件复制操作,确保在完成操作后正确关闭资源。
javaCopy code
import java.io.*;
?
public class FileCopyUtility {
?
? public static void main(String[] args) {
? ? ? String sourceFilePath = "source.txt";
? ? ? String destinationFilePath = "destination.txt";
?
? ? ? // 使用文件复制工具类进行文件复制
? ? ? try {
? ? ? ? ? copyFile(sourceFilePath, destinationFilePath);
? ? ? ? ? System.out.println("File copy completed.");
? ? ? } catch (IOException e) {
? ? ? ? ? System.err.println("Error during file copy: " + e.getMessage());
? ? ? }
? }
?
? public static void copyFile(String sourcePath, String destinationPath) throws IOException {
? ? ? // 使用 try-with-resources 语句创建文件输入流和输出流
? ? ? try (FileInputStream inputStream = new FileInputStream(sourcePath);
? ? ? ? ? ? FileOutputStream outputStream = new FileOutputStream(destinationPath)) {
?
? ? ? ? ? // 使用缓冲区进行文件复制操作
? ? ? ? ? byte[] buffer = new byte[4096];
? ? ? ? ? int bytesRead;
? ? ? ? ? while ((bytesRead = inputStream.read(buffer)) != -1) {
? ? ? ? ? ? ? outputStream.write(buffer, 0, bytesRead);
? ? ? ? ? }
?
? ? ? } catch (IOException e) {
? ? ? ? ? // 如果在复制文件时发生异常,将异常传播给调用者
? ? ? ? ? throw e;
? ? ? }
? ? ? // 文件输入流和输出流会在这里自动关闭,无需手动关闭
? }
}
在这个例子中,FileCopyUtility 类包含一个 copyFile 方法,该方法使用 FileInputStream 读取源文件内容,并使用 FileOutputStream 写入目标文件。通过将这两个流放在 try-with-resources 语句中,可以确保在复制完成后正确关闭这两个资源,无需显式调用 close 方法。
这种文件复制实战例子展示了如何结合 Closeable 接口和 try-with-resources 语句来处理文件操作,提高代码的可读性和可靠性。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!