Java中try-with-resources的使用詳解
try-with-resources 是 Java 7 引入的語法糖,用于 自動 管理資源(如文件流、數(shù)據(jù)庫連接等),確保資源在使用后被正確關(guān)閉,避免資源泄漏。
語法糖 是什么?語法糖 (Syntactic Sugar)是計(jì)算機(jī)語言中對語法進(jìn)行的一些改動,使得代碼更易于人類閱讀和編寫,但并不改變語言的功能或表達(dá)能力。本質(zhì)上,編譯器 / 解釋器會在背后把這些 "甜語法" 轉(zhuǎn)換成語言原本的基礎(chǔ)語法來執(zhí)行。
回到我們的 try-with-resources。
基本語法:
try (ResourceType resource = new ResourceType()) {
// 使用資源的代碼
} catch (Exception e) {
// 異常處理
}
資源類 必須實(shí)現(xiàn) AutoCloseable 或 Closeable 接口:
public interface AutoCloseable {
void close() throws Exception;
}
public interface Closeable extends AutoCloseable {
void close() throws IOException;
}
傳統(tǒng) finally 方式(繁瑣易出錯):
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("test.txt"));
// 業(yè)務(wù)代碼
} catch (IOException e) {
e.printStackTrace();
} finally {
// 必須判空 + 嵌套try-catch(close()也會拋異常)
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try-with-resources 方式(簡潔健壯):
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
// 業(yè)務(wù)代碼
} catch (Exception e) {
e.printStackTrace();
}
單個資源:
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
多個資源(支持在括號內(nèi)聲明多個資源,用;分隔):
try (InputStream in = new FileInputStream("input.txt");
OutputStream out = new FileOutputStream("output.txt")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
資源順序(多個資源按聲明順序關(guān)閉 - 后聲明的先關(guān)閉):
// 聲明順序:Connection → PreparedStatement → ResultSet
try (Connection conn = DriverManager.getConnection(url, user, pwd);
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user");
ResultSet rs = ps.executeQuery()) {
// 業(yè)務(wù)邏輯
}
// 關(guān)閉順序:ResultSet → PreparedStatement → Connection
try-with-resources 是 Java 開發(fā)中資源管理的首選方式,大大減少了資源泄漏的風(fēng)險(xiǎn),提高了代碼的可讀性和可維護(hù)性。
到此這篇關(guān)于Java中try-with-resources的使用詳解的文章就介紹到這了,更多相關(guān)Java try-with-resources內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java中try-with-resources使用教程
- Java中try-with-resources自動關(guān)閉資源的使用方法及注意事項(xiàng)
- Java使用try-with-resources實(shí)現(xiàn)自動解鎖
- 關(guān)于Java中的try-with-resources語句
- Java異常--常見方法--自定義異常--增強(qiáng)try(try-with-resources)詳解
- java面試try-with-resources問題解答
- Java try()語句實(shí)現(xiàn)try-with-resources異常管理機(jī)制操作
- Java使用 try-with-resources 實(shí)現(xiàn)自動關(guān)閉資源的方法
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)常見幾大排序梳理
Java常見的排序算法有:直接插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序等。本文詳解介紹它們的實(shí)現(xiàn)以及圖解,需要的可以參考一下2022-03-03
Spring實(shí)戰(zhàn)之使用Resource作為屬性操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用Resource作為屬性,結(jié)合實(shí)例形式分析了spring載人Resource作為屬性相關(guān)配置與使用技巧,需要的朋友可以參考下2020-01-01
SpringBoot啟動時(shí)自動執(zhí)行指定方法的幾種實(shí)現(xiàn)方式
在Spring Boot應(yīng)用程序中,要實(shí)現(xiàn)在應(yīng)用啟動時(shí)自動執(zhí)行某些代碼,本文主要介紹了SpringBoot啟動時(shí)自動執(zhí)行指定方法的幾種方式,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-03-03
Spring Boot定制type Formatters實(shí)例詳解
在本篇文章里小編給大家整理的是關(guān)于Spring Boot定制type Formatters實(shí)例知識點(diǎn),需要的朋友們學(xué)習(xí)下。2019-11-11

