JavaEE學習日誌持續更新----> 必看!JavaEE學習路線(文章總彙)
Java學習日誌(二十)
IO異常處理
JDK7之前的IO異常處理
JDK7之前:使用try_catch_finally使用流中的異常
格式:
try{
可能會產生異常的代碼
}catch(Exception e){
異常的處理邏輯
}finally{
一定會執行的代碼,一般用於資源釋放
}
代碼示例:
public class Demo01 {
public static void main(String[] args) {
//提高變量作用域,方便finally可以使用
FileWriter fw = null;
try {
//可能會產生異常的代碼
fw = new FileWriter("day10\\6.txt", true);
for (int i = 0; i < 10; i++) {
fw.write("hello" + i + "\r\n");
}
} catch (Exception e) {
//異常的處理邏輯
e.printStackTrace();
} finally {
//一定會執行的代碼,一般用於資源釋放
/*
Variable 'fw' might not have been initialized
沒有對變量賦初始值,不能使用
*/
/*
close方法本身有異常,需要繼續try...catch
*/
/*
如果創建FileWriter對象失敗,fw變量初始值爲null
而null不能調用方法,所以fw.close()會拋出空指針異常
*/
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:如果沒有fw!= null
的邏輯判斷,則可能會出現空指針異常,因爲null不能調用方法!
JDK7之後的IO異常處理
JDK7之後:使用try_catch使用流中的異常
新特性:在try的後邊可以增加一個(),在()中可以定義流對象,這樣流對象的作用域就在()中有效,()中的代碼執行完畢,那麼流對象就會自動釋放,不用寫finally
格式:
try(定義流對象;...){
可能會產生異常的代碼
}catch(Exception e){
異常的處理邏輯
}
代碼示例:文件複製
public class Demo02 {
public static void main(String[] args) {
long s = System.currentTimeMillis();
try (FileInputStream fis = new FileInputStream("c:\\1.jpg");
FileOutputStream fos = new FileOutputStream("d:\\1.jpg");) {
byte[] bytes = new byte[1024 * 120];
int len = 0;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
long e = System.currentTimeMillis();
System.out.println("複製文件共耗時:" + (e - s) + "ms");
}
}
屬性集(Properties)
Properties集合的基本使用
java.util.Properties集合 extend Hashtable<K,V> implements Map<K,V>
Properties集合的特點:
- Properties類表示一組持久的屬性集。 Properties可以保存到流中或從流中加載。
- Properties是一個唯一和IO流相結合的集合
使用Properties集合中的方法store,可以把集合中的臨時數據,寫入到文件中,持久化存儲
使用Properties集合中的方法load,把文件中保存的鍵值對,讀取到集合中使用 - 屬性列表中的每個鍵及其對應的值都是一個字符串。
Properties集合鍵和值默認都使用String類型
Properties集合的基本使用:存儲數據,遍歷集合取出數據
Properties鍵和值都是String,包含了一些操作字符串的特有方法
Object setProperty(String key, String value)
往Properties集合中添加鍵值對,此方法相當於map集合的put方法String getProperty(String key)
通過key獲取value,此方法相當於map集合的get方法Set<String> stringPropertyNames()
相當於map集合的keySet方法
代碼示例:
private static void show01() {
//創建Properties集合對象
Properties prop = new Properties();
//setProperty方法,往集合中添加鍵值對
prop.setProperty("aaa","20");
prop.setProperty("bbb","22");
prop.setProperty("ccc","24");
prop.setProperty("ddd","26");
//stringPropertyNames,把所有的key取出來存儲到一個set集合中
Set<String> set = prop.stringPropertyNames();
//遍歷set集合
for (String key : set) {
//getProperty方法,通過key獲取value
String value = prop.getProperty(key);
System.out.println("key:"+key+" value:"+value);
}
}
Properties集合的store方法
作用:使用Properties集合中的方法store,可以把集合中的臨時數據,寫入到文件中,持久化存儲。
Properties集合的store方法:
-
void store(OutputStream out, String comments)
-
void store(Writer writer, String comments)
參數:
OutputStream out:字節輸出流,不能寫中文
Writer writer:字符輸出流,可以寫中文
String comments:註釋,解釋說明保存的文件,不能寫中文,默認使用Unicode編碼(會有亂碼)
代碼示例:
private static void show02() throws IOException {
//創建Properties集合對象
Properties prop = new Properties();
//setProperty方法,往集合中添加鍵值對
prop.setProperty("aaa","20");
prop.setProperty("bbb","22");
prop.setProperty("ccc","24");
prop.setProperty("噠噠噠","26");
//使用Properties集合中的方法,可以把集合中的臨時數據,寫入到文件中,持久化存儲
prop.store(new FileOutputStream("day10\\prop1.txt"),"save date");
prop.store(new FileWriter("day10\\prop2.txt"),"save date");
}
Properties集合的load方法
作用:使用Properties集合中的方法load,把文件中保存的鍵值對,讀取到集合中使用
Properties集合的load方法:
void load(InputStream inStream)
從輸入字節流中讀取屬性列表(鍵和元素對)。void load(Reader reader)
以簡單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對)。
參數:
InputStream inStream:字節輸入流,不能讀取含有中文的文件
Reader reader:字符輸入流,能讀取含有中文的文件
注意事項:
- 在保存鍵值對的文件中,可以使用#添加註釋,被註釋的鍵值對,不會被讀取
- 在保存鍵值對的文件中,鍵與值的連接符號可以使用=或者空格
- 在保存鍵值對的文件中,鍵與值默認都是String類型,不要添加引號
代碼示例:
private static void show03() throws IOException {
//創建Properties集合對象
Properties prop = new Properties();
//使用Properties集合中的方法load,把文件中保存的鍵值對,讀取到集合中使用
//prop.load(new FileInputStream("day10\\prop2.txt"));
prop.load(new FileReader("day10\\prop2.txt"));
//遍歷集合
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}