Java?IO異常如何處理詳析
JDK7前處理
之前的練習(xí),我們一直把異常拋出,而實(shí)際開(kāi)發(fā)中并不能這樣處理,建議使用try...catch...finally 代碼塊,處理異常部分,代碼使用演示:
public class HandleException1 { public static void main(String[] args) { // 聲明變量 FileWriter fw = null; try { //創(chuàng)建流對(duì)象 fw = new FileWriter("fw.txt"); // 寫(xiě)出數(shù)據(jù) fw.write("三分歸元?dú)?); //三分歸元?dú)? } catch (IOException e) { e.printStackTrace(); } finally { try { if (fw != null) { fw.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
JDK7的處理(擴(kuò)展知識(shí)點(diǎn)了解內(nèi)容)
還可以使用JDK7優(yōu)化后的try-with-resource 語(yǔ)句,該語(yǔ)句確保了每個(gè)資源在語(yǔ)句結(jié)束時(shí)關(guān)閉。所謂的資源(resource)是指在程序完成后,必須關(guān)閉的對(duì)象。
格式:
try (創(chuàng)建流對(duì)象語(yǔ)句,如果多個(gè),使用';'隔開(kāi)) { // 讀寫(xiě)數(shù)據(jù) } catch (IOException e) { e.printStackTrace(); }
代碼使用演示:
public class HandleException2 { public static void main(String[] args) { // 創(chuàng)建流對(duì)象 try ( FileWriter fw = new FileWriter("fw.txt"); ) { // 寫(xiě)出數(shù)據(jù) fw.write("三分歸元?dú)?); //三分歸元?dú)? } catch (IOException e) { e.printStackTrace(); } } }
JDK9的改進(jìn)(擴(kuò)展知識(shí)點(diǎn)了解內(nèi)容)
JDK9中try-with-resource 的改進(jìn),對(duì)于引入對(duì)象的方式,支持的更加簡(jiǎn)潔。被引入的對(duì)象,同樣可以自動(dòng)關(guān)閉,無(wú)需手動(dòng)close,我們來(lái)了解一下格式。
改進(jìn)前格式:
// 被final修飾的對(duì)象 final Resource resource1 = new Resource("resource1"); // 普通對(duì)象 Resource resource2 = new Resource("resource2"); // 引入方式:創(chuàng)建新的變量保存 try (Resource r1 = resource1; Resource r2 = resource2) { // 使用對(duì)象 }
改進(jìn)后格式:
// 被final修飾的對(duì)象 final Resource resource1 = new Resource("resource1"); // 普通對(duì)象 Resource resource2 = new Resource("resource2"); // 引入方式:直接引入 try (resource1; resource2) { ? ? ?// 使用對(duì)象 }
改進(jìn)后,代碼使用演示:
public class TryDemo { public static void main(String[] args) throws IOException { // 創(chuàng)建流對(duì)象 final FileReader fr = new FileReader("in.txt"); FileWriter fw = new FileWriter("out.txt"); // 引入到try中 try (fr; fw) { // 定義變量 int b; // 讀取數(shù)據(jù) while ((b = fr.read())!=-1) { // 寫(xiě)出數(shù)據(jù) fw.write(b); } } catch (IOException e) { e.printStackTrace(); } } }
屬性集
概述
java.util.Properties 繼承于Hashtable ,來(lái)表示一個(gè)持久的屬性集。它使用鍵值結(jié)構(gòu)存儲(chǔ)數(shù)據(jù),每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串。該類(lèi)也被許多Java類(lèi)使用,比如獲取系統(tǒng)屬性時(shí),System.getProperties 方法就是返回一個(gè)Properties對(duì)象。
Properties類(lèi)
構(gòu)造方法
- public Properties() :創(chuàng)建一個(gè)空的屬性列表。
基本的存儲(chǔ)方法
- public Object setProperty(String key, String value) : 保存一對(duì)屬性。
- public String getProperty(String key) :使用此屬性列表中指定的鍵搜索屬性值。
- public Set<String> stringPropertyNames() :所有鍵的名稱(chēng)的集合。
public class ProDemo { ? ? public static void main(String[] args) throws FileNotFoundException { ? ? ? ? // 創(chuàng)建屬性集對(duì)象 ? ? ? ? Properties properties = new Properties(); ? ? ? ? // 添加鍵值對(duì)元素 ? ? ? ? properties.setProperty("filename", "a.txt"); ? ? ? ? properties.setProperty("length", "209385038"); ? ? ? ? properties.setProperty("location", "D:\\a.txt"); ? ? ? ? // 打印屬性集對(duì)象 ? ? ? ? System.out.println(properties); ? ? ? ? // 通過(guò)鍵,獲取屬性值 ? ? ? ? System.out.println(properties.getProperty("filename")); ? ? ? ? System.out.println(properties.getProperty("length")); ? ? ? ? System.out.println(properties.getProperty("location")); ? ? ? ? // 遍歷屬性集,獲取所有鍵的集合 ? ? ? ? Set<String> strings = properties.stringPropertyNames(); ? ? ? ? // 打印鍵值對(duì) ? ? ? ? for (String key : strings ) { ? ? ? ? ? ?? ?System.out.println(key+" -- "+properties.getProperty(key)); ? ? ? ? } ? ? } }
輸出結(jié)果:
{filename=a.txt, length=209385038, location=D:\a.txt}
a.txt
209385038
D:\a.txt
filename -- a.txt
length -- 209385038
location -- D:\a.txt
與流相關(guān)的方法
- public void load(InputStream inStream): 從字節(jié)輸入流中讀取鍵值對(duì)。
參數(shù)中使用了字節(jié)輸入流,通過(guò)流對(duì)象,可以關(guān)聯(lián)到某文件上,這樣就能夠加載文本中的數(shù)據(jù)了。文本數(shù)據(jù)格式:
filename=a.txt
length=209385038
location=D:\a.txt
加載代碼演示:
public class ProDemo2 { ? ? public static void main(String[] args) throws FileNotFoundException { ? ? ? ? // 創(chuàng)建屬性集對(duì)象 ? ? ? ? Properties pro = new Properties(); ? ? ? ? // 加載文本中信息到屬性集 ? ? ? ? pro.load(new FileInputStream("read.txt")); ? ? ? ? // 遍歷集合并打印 ? ? ? ? Set<String> strings = pro.stringPropertyNames(); ? ? ? ? for (String key : strings ) { ? ? ? ? ? ?? ?System.out.println(key+" -- "+pro.getProperty(key)); ? ? ? ? } ? ? ?} }
輸出結(jié)果:
filename -- a.txt
length -- 209385038
location -- D:\a.txt
總結(jié)
到此這篇關(guān)于Java IO異常如何處理的文章就介紹到這了,更多相關(guān)Java IO異常處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring配置文件解析之BeanDefinitionReader詳解
這篇文章主要介紹了Spring配置文件解析之BeanDefinitionReader詳解,ApplicationContext.xml配置文件解析成Document對(duì)象,真正對(duì)xml中元素解析的類(lèi)是在BeanDefinitionDocumentReader的實(shí)現(xiàn)類(lèi)中來(lái)完成的,需要的朋友可以參考下2024-02-02MyBatis自定義TypeHandler如何解決字段映射問(wèn)題
這篇文章主要介紹了MyBatis自定義TypeHandler如何解決字段映射問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12SpringCloud Feign傳遞HttpServletRequest對(duì)象流程
HttpServletRequest接口的對(duì)象代表客戶端的請(qǐng)求,當(dāng)客戶端通過(guò)HTTP協(xié)議訪問(wèn)Tomcat服務(wù)器時(shí),HTTP請(qǐng)求中的所有信息都封裝在HttpServletRequest接口的對(duì)象中,這篇文章介紹了Feign傳遞HttpServletRequest對(duì)象的流程,感興趣的同學(xué)可以參考下文2023-05-05Spring Boot整合mybatis并自動(dòng)生成mapper和實(shí)體實(shí)例解析
本文是小編給大家總結(jié)的關(guān)于Spring Boot整合mybatis并自動(dòng)生成mapper和實(shí)體的內(nèi)容,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05Java8 實(shí)現(xiàn)stream將對(duì)象集合list中抽取屬性集合轉(zhuǎn)化為map或list
這篇文章主要介紹了Java8 實(shí)現(xiàn)stream將對(duì)象集合list中抽取屬性集合轉(zhuǎn)化為map或list的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02java并發(fā)編程專(zhuān)題(一)----線程基礎(chǔ)知識(shí)
這篇文章主要介紹了java并發(fā)編程線程的基礎(chǔ)知識(shí),文中講解非常詳細(xì),幫助大家更好的學(xué)習(xí)JAVA并發(fā)編程,感興趣想學(xué)習(xí)JAVA的可以了解下2020-06-06