欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java?IO異常如何處理詳析

 更新時(shí)間:2022年12月29日 16:04:05   作者:共飲一杯無  
異常就是Java程序在運(yùn)行過程中出現(xiàn)的錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于Java?IO異常如何處理的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

JDK7前處理

之前的練習(xí),我們一直把異常拋出,而實(shí)際開發(fā)中并不能這樣處理,建議使用try...catch...finally 代碼塊,處理異常部分,代碼使用演示:

public class HandleException1 {
    public static void main(String[] args) {
          // 聲明變量
        FileWriter fw = null;
        try {
            //創(chuàng)建流對象
            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ò)展知識點(diǎn)了解內(nèi)容)

還可以使用JDK7優(yōu)化后的try-with-resource 語句,該語句確保了每個(gè)資源在語句結(jié)束時(shí)關(guān)閉。所謂的資源(resource)是指在程序完成后,必須關(guān)閉的對象。

格式:

try (創(chuàng)建流對象語句,如果多個(gè),使用';'隔開) {
    // 讀寫數(shù)據(jù)
} catch (IOException e) {
    e.printStackTrace();
}

代碼使用演示:

public class HandleException2 {
    public static void main(String[] args) {
          // 創(chuàng)建流對象
        try ( FileWriter fw = new FileWriter("fw.txt"); ) {
            // 寫出數(shù)據(jù)
            fw.write("三分歸元?dú)?); //三分歸元?dú)?
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

JDK9的改進(jìn)(擴(kuò)展知識點(diǎn)了解內(nèi)容)

JDK9中try-with-resource 的改進(jìn),對于引入對象的方式,支持的更加簡潔。被引入的對象,同樣可以自動關(guān)閉,無需手動close,我們來了解一下格式。

改進(jìn)前格式:

// 被final修飾的對象
final Resource resource1 = new Resource("resource1");
// 普通對象
Resource resource2 = new Resource("resource2");
// 引入方式:創(chuàng)建新的變量保存
try (Resource r1 = resource1;
     Resource r2 = resource2) {
     // 使用對象
}

改進(jìn)后格式:

// 被final修飾的對象
final Resource resource1 = new Resource("resource1");
// 普通對象
Resource resource2 = new Resource("resource2");

// 引入方式:直接引入
try (resource1; resource2) {
? ? ?// 使用對象
}

改進(jìn)后,代碼使用演示:

public class TryDemo {
    public static void main(String[] args) throws IOException {
           // 創(chuàng)建流對象
        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)存儲數(shù)據(jù),每個(gè)鍵及其對應(yīng)值都是一個(gè)字符串。該類也被許多Java類使用,比如獲取系統(tǒng)屬性時(shí),System.getProperties 方法就是返回一個(gè)Properties對象。

Properties類

構(gòu)造方法

  • public Properties() :創(chuàng)建一個(gè)空的屬性列表。

基本的存儲方法

  • public Object setProperty(String key, String value) : 保存一對屬性。
  • public String getProperty(String key) :使用此屬性列表中指定的鍵搜索屬性值。
  • public Set<String> stringPropertyNames() :所有鍵的名稱的集合。
public class ProDemo {
? ? public static void main(String[] args) throws FileNotFoundException {
? ? ? ? // 創(chuàng)建屬性集對象
? ? ? ? Properties properties = new Properties();
? ? ? ? // 添加鍵值對元素
? ? ? ? properties.setProperty("filename", "a.txt");
? ? ? ? properties.setProperty("length", "209385038");
? ? ? ? properties.setProperty("location", "D:\\a.txt");
? ? ? ? // 打印屬性集對象
? ? ? ? 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();
? ? ? ? // 打印鍵值對
? ? ? ? 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é)輸入流中讀取鍵值對。

參數(shù)中使用了字節(jié)輸入流,通過流對象,可以關(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)建屬性集對象
? ? ? ? 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis自定義TypeHandler如何解決字段映射問題

    MyBatis自定義TypeHandler如何解決字段映射問題

    這篇文章主要介紹了MyBatis自定義TypeHandler如何解決字段映射問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • SpringCloud Feign傳遞HttpServletRequest對象流程

    SpringCloud Feign傳遞HttpServletRequest對象流程

    HttpServletRequest接口的對象代表客戶端的請求,當(dāng)客戶端通過HTTP協(xié)議訪問Tomcat服務(wù)器時(shí),HTTP請求中的所有信息都封裝在HttpServletRequest接口的對象中,這篇文章介紹了Feign傳遞HttpServletRequest對象的流程,感興趣的同學(xué)可以參考下文
    2023-05-05
  • Spring Boot整合mybatis并自動生成mapper和實(shí)體實(shí)例解析

    Spring Boot整合mybatis并自動生成mapper和實(shí)體實(shí)例解析

    本文是小編給大家總結(jié)的關(guān)于Spring Boot整合mybatis并自動生成mapper和實(shí)體的內(nèi)容,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-05-05
  • Java8 實(shí)現(xiàn)stream將對象集合list中抽取屬性集合轉(zhuǎn)化為map或list

    Java8 實(shí)現(xiàn)stream將對象集合list中抽取屬性集合轉(zhuǎn)化為map或list

    這篇文章主要介紹了Java8 實(shí)現(xiàn)stream將對象集合list中抽取屬性集合轉(zhuǎn)化為map或list的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • java并發(fā)編程專題(一)----線程基礎(chǔ)知識

    java并發(fā)編程專題(一)----線程基礎(chǔ)知識

    這篇文章主要介紹了java并發(fā)編程線程的基礎(chǔ)知識,文中講解非常詳細(xì),幫助大家更好的學(xué)習(xí)JAVA并發(fā)編程,感興趣想學(xué)習(xí)JAVA的可以了解下
    2020-06-06
  • 關(guān)于Java中的CAS如何使用

    關(guān)于Java中的CAS如何使用

    這篇文章主要介紹了關(guān)于Java中的CAS如何使用,CAS是Compare And Swap(比較并交換)的縮寫,是一種非阻塞式并發(fā)控制技術(shù),用于保證多個(gè)線程在修改同一個(gè)共享資源時(shí)不會出現(xiàn)競爭條件,從而避免了傳統(tǒng)鎖機(jī)制的各種問題,需要的朋友可以參考下
    2023-09-09
  • 最新評論