190 likes | 285 Views
Java 的例外與輸入出檔案處理. Jing Ming Huang. Java 的例外處理. 例外( Exception): 程式執行時,發生不正常執行狀態時所發生的事件 架構:. 產生錯誤的例外 A. MethodC() 方法. 找尋例外處理. MethodB() 方法. 自行丟出例外 B 到 main. MethodA() 方法. 擁有例外處理 A. main() 方法. 擁有例外處理 B. 13-1-2 Throwable 類別.
E N D
Java 的例外與輸入出檔案處理 Jing Ming Huang
Java的例外處理 • 例外(Exception):程式執行時,發生不正常執行狀態時所發生的事件 • 架構: 產生錯誤的例外A MethodC()方法 找尋例外處理 MethodB()方法 自行丟出例外B到main MethodA()方法 擁有例外處理A main()方法 擁有例外處理B
13-1-2 Throwable類別 • Throwable類別擁有兩個直接的子類別: Error類別:屬於JVM的嚴重錯誤,將導致程式的終止執行,並沒有辦法使用例外處理來處理錯誤 Exception類別:其子類別是各種例外物件,也是例外處理可以處理的部份
例外處理的程式敘述 • 敘述分為三個程式區塊:try、 catch、 finally,可以建立程式的例外處理 try { ………………} catch(ExceptionType e) { //例外處理 …………… } finally{……………..} 檢查是否發生例外 參數e為例外類型的物件 一個程式選項,區塊進行程式的善後 Ex:Ch13_2_1.java
13-2-2同時處理多種例外 • 可使用多個catch程式區塊,同時處理多種不同的例外 try{ } catch(ArithmeticException e ) {………..} catch(ArrayIndexOutOfBoundsException e ) {………..} finally{ } Ex:Ch13_2_2.java
13-3丟出例外與自訂Exception類別 • 方法本身也可以自行丟出例外,或當例外產生時,丟給其他的方法 • 13-3-1 使用throw指令丟出例外 語法: throw ThrowableObject; ex:丟出ArithmeticException的例外物件 throw new ArithmeticException("值小於5"); 繼承自Throwable類別的物件 使用new建立例外物件 建構子參數是讓getMessage()方法取得的例外說明字串 Ex:Ch13_3_1.java
13-3-2在方法丟出例外 static double cal (double a, double b,double c)throws IllegalArgumentException { ………….. throw new IllegalArgumentException(〝c不能是0!〞); } Ex:Ch13_3_2.java
自訂Exception類別 class MyException extends Exception { int data; public MyException(int data) { this.data = data; } public String getMessage() { return ("出價太多次: " + data); } } Ex:Ch13_3_3.java
13-4 Java的輸入/輸出串流 • 13-4-1 串流的基礎 讀取 寫入 程式 目的資料 來源資料 輸入串流 輸出串流
13-4-2 java.io套件的串流類別 • 分為兩大類: 字元串流(Character Stream) 位元組串流(Byte Stream) • 字元串流: 適合“人類閱讀”的串流 BufferReader/BufferWriter:處理緩衝區I/O InputStreamReader/OutputStreamWriter: InputStreamReadery在讀取位元組資料後,可以將它轉成字元組資料,OutputStreamWriter是將字元轉成位元組資料 FileReader/FileWriter:處理檔案I/O
位元組串流: “電腦格式“串流可處理二進位資料的執行檔 FileInputStream/FileOutputStream:處理檔案I/O BufferInputStream/BufferOutputStream:處理緩衝區I/O DataInputStream/DataOutputStream:讀取或寫入java基本資料型態的資料
13-5標準輸出與輸入串流 • ->指System類別的System.in和System.out子類別,可以從標準輸入讀取資料和輸出到標準輸出
13-5-2標準輸出 • 將資料輸出到螢幕,可開啟System.out標準輸出的OutputStreamWriter串流然後使用緩衝器BufferedWriter串流來加速資料處理 BufferedWriter output = new BufferedWriter( new OutputStreamWriter(System.out)); Ex:Ch13_5_2.java
13-5-3 標準輸入 • 從鍵盤輸入資料:開System.in標準輸出的Input StreamReader串流,然後使用緩衝區Bufferedreader串流加速資料的處理 BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); Ex:Ch13_5_3.java
13-6 Reader/Writer檔案串流 • 13-6-1 寫入文字檔案 目標串流:檔案 用FileWriter串流開啟檔案,以BufferedWriter串流加速資料的處理 BufferedWriter Output = new BufferedWriter(new FileWriter(file)); Ex:Ch13_6_1.java
13-6-2 讀取文字檔案 目標串流:檔案 用FilerReader串流開啟檔案,以BufferedReader串流加速資料的處理 BufferedReader input = new BufferedReader(new FileReader(file)); • 13-6-3 檔案複製 整合前兩節範例就可以建立檔案複製程式 Ex:Ch13_6_2.java Ex:Ch13_6_3.java
13-7 InputSteam/OutputStream檔案串流 • 處理二進位檔案:使用位元組串流的InputStream/OutStream類別處理檔案的讀取和寫入
位元組串流要轉換成字元串流:使用InputStreamWriteer/InputStreamReader串流當過濾器,過濾成字元串流位元組串流要轉換成字元串流:使用InputStreamWriteer/InputStreamReader串流當過濾器,過濾成字元串流 BufferedOutputStream bos=new BufferedOutputStream( new FileOutputStream(file)); OutputStreamWriter output = new OutputStreamWriter(bos); …………………………………… BufferedInputStream bis=new BufferedInputStream( new FileInputStream(file)); InputStreamReader input = new InputStreamReader(bis); ex:Ch13_7.java
13-8 隨機存取檔案的處理 • 使用非順序和隨機方式來存取檔案內容,可以使用索引存取元素,這個索引稱為「檔案指標」。 RandomAccessFile input = new RandomAccessFile(file,"rw"); RandomAccessFile input = new RandomAccessFile(file,"r"); Ex:Ch13_8.java