Java?IO異常如何處理詳析
JDK7前處理
之前的練習(xí),我們一直把異常拋出,而實(shí)際開發(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");
// 寫出數(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 語句,該語句確保了每個(gè)資源在語句結(jié)束時(shí)關(guān)閉。所謂的資源(resource)是指在程序完成后,必須關(guān)閉的對(duì)象。
格式:
try (創(chuàng)建流對(duì)象語句,如果多個(gè),使用';'隔開) {
// 讀寫數(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"); ) {
// 寫出數(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ì)象的方式,支持的更加簡潔。被引入的對(duì)象,同樣可以自動(dòng)關(guān)閉,無需手動(dòng)close,我們來了解一下格式。
改進(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) {
// 寫出數(shù)據(jù)
fw.write(b);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
屬性集
概述
java.util.Properties 繼承于Hashtable ,來表示一個(gè)持久的屬性集。它使用鍵值結(jié)構(gòu)存儲(chǔ)數(shù)據(jù),每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串。該類也被許多Java類使用,比如獲取系統(tǒng)屬性時(shí),System.getProperties 方法就是返回一個(gè)Properties對(duì)象。
Properties類
構(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() :所有鍵的名稱的集合。
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);
? ? ? ? // 通過鍵,獲取屬性值
? ? ? ? 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é)輸入流,通過流對(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中元素解析的類是在BeanDefinitionDocumentReader的實(shí)現(xiàn)類中來完成的,需要的朋友可以參考下2024-02-02
MyBatis自定義TypeHandler如何解決字段映射問題
SpringCloud Feign傳遞HttpServletRequest對(duì)象流程
Spring Boot整合mybatis并自動(dòng)生成mapper和實(shí)體實(shí)例解析
Java8 實(shí)現(xiàn)stream將對(duì)象集合list中抽取屬性集合轉(zhuǎn)化為map或list
java并發(fā)編程專題(一)----線程基礎(chǔ)知識(shí)

