Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java 的例外與輸入出檔案處理 Jing Ming Huang.

Similar presentations


Presentation on theme: "Java 的例外與輸入出檔案處理 Jing Ming Huang."— Presentation transcript:

1 Java 的例外與輸入出檔案處理 Jing Ming Huang

2 Java的例外處理 例外(Exception):程式執行時,發生不正常執行狀態時所發生的事件 架構: 產生錯誤的例外A
MethodC()方法 產生錯誤的例外A 找尋例外處理 MethodB()方法 自行丟出例外B到main MethodA()方法 擁有例外處理A main()方法 擁有例外處理B

3 Throwable類別 Throwable類別擁有兩個直接的子類別: Error類別:屬於JVM的嚴重錯誤,將導致程式的終止執行,並沒有辦法使用例外處理來處理錯誤 Exception類別:其子類別是各種例外物件,也是例外處理可以處理的部份 ArithmeticException 數學運算時產生的例外 ArrayIndexOutOfBoundsException 陣列索引值小於0或超過陣列邊界所產生的例外 IllegalArgumentException 儲存陣列元素時型態不符產生的例外 NullPointerException 物件值為null產生的例外

4 例外處理的程式敘述 Ex:Ch13_2_1.java 敘述分為三個程式區塊:try、 catch、 finally,可以建立程式的例外處理
catch(ExceptionType e) { //例外處理 …………… } finally{……………..} 檢查是否發生例外 參數e為例外類型的物件 一個程式選項,區塊進行程式的善後 Ex:Ch13_2_1.java

5 13-2-2同時處理多種例外 Ex:Ch13_2_2.java 可使用多個catch程式區塊,同時處理多種不同的例外 try{ }
catch(ArithmeticException e ) {………..} catch(ArrayIndexOutOfBoundsException e ) {………..} finally{ } Ex:Ch13_2_2.java

6 13-3丟出例外與自訂Exception類別 Ex:Ch13_3_1.java 方法本身也可以自行丟出例外,或當例外產生時,丟給其他的方法
使用throw指令丟出例外 語法: throw ThrowableObject; ex:丟出ArithmeticException的例外物件 throw new ArithmeticException("值小於5"); 繼承自Throwable類別的物件 使用new建立例外物件 建構子參數是讓getMessage()方法取得的例外說明字串 Ex:Ch13_3_1.java

7 13-3-2在方法丟出例外 Ex:Ch13_3_2.java
static double cal (double a, double b,double c)throws IllegalArgumentException { ………….. throw new IllegalArgumentException(〝c不能是0!〞); } Ex:Ch13_3_2.java

8 自訂Exception類別 Ex:Ch13_3_3.java class MyException extends Exception {
int data; public MyException(int data) this.data = data; } public String getMessage() return ("出價太多次: " + data); Ex:Ch13_3_3.java

9 13-4 Java的輸入/輸出串流 串流的基礎 讀取 寫入 來源資料 程式 目的資料 輸入串流 輸出串流

10 13-4-2 java.io套件的串流類別 分為兩大類: 字元串流(Character Stream) 位元組串流(Byte Stream)
字元串流: 適合“人類閱讀”的串流 BufferReader/BufferWriter:處理緩衝區I/O InputStreamReader/OutputStreamWriter: InputStreamReadery在讀取位元組資料後,可以將它轉成字元組資料,OutputStreamWriter是將字元轉成位元組資料 FileReader/FileWriter:處理檔案I/O

11 位元組串流: “電腦格式“串流可處理二進位資料的執行檔 FileInputStream/FileOutputStream:處理檔案I/O
BufferInputStream/BufferOutputStream:處理緩衝區I/O DataInputStream/DataOutputStream:讀取或寫入java基本資料型態的資料

12 13-5標準輸出與輸入串流 int read () int read (char[])
->指System類別的System.in和System.out子類別,可以從標準輸入讀取資料和輸出到標準輸出 int read () 讀取1個字元 int read (char[]) 讀取字元陣列,當到資料資料結尾時,傳回-1 int read (char[],int,int) 讀取從第2個參數int索引值開始長度為第3個參數int的字元陣列 void write(int) 寫入整數 void write(char[]) 寫入1個字元陣列 void write(char[],int,int) 寫入從第2個參數int索引值開始長度第3個參數int的字元陣列 void flush() 清除串流,也是將串流資料全部輸出 void close() 關閉串流

13 13-5-2標準輸出 將資料輸出到螢幕,可開啟System.out標準輸出的OutputStreamWriter串流然後使用緩衝器BufferedWriter串流來加速資料處理 BufferedWriter output = new BufferedWriter( new OutputStreamWriter(System.out)); void write(String) 寫入1個字串 void write(String,int,int) 寫入從第2個參數int索引值開始長度為第3個參數int的字串 Ex:Ch13_5_2.java

14 13-5-3 標準輸入 Ex:Ch13_5_3.java String readLine()
從鍵盤輸入資料:開System.in標準輸出的Input StreamReader串流,然後使用緩衝區Bufferedreader串流加速資料的處理 BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String readLine() 讀取1行「 \r 」、 「 \n 」 、「 \r\n 」結尾的文字 Ex:Ch13_5_3.java

15 13-6 Reader/Writer檔案串流 Ex:Ch13_6_1.java
寫入文字檔案 目標串流:檔案 用FileWriter串流開啟檔案,以BufferedWriter串流加速資料的處理 BufferedWriter Output = new BufferedWriter(new FileWriter(file)); Ex:Ch13_6_1.java

16 Ex:Ch13_6_2.java Ex:Ch13_6_3.java
讀取文字檔案 目標串流:檔案 用FilerReader串流開啟檔案,以BufferedReader串流加速資料的處理 BufferedReader input = new BufferedReader(new FileReader(file)); 檔案複製 整合前兩節範例就可以建立檔案複製程式 Ex:Ch13_6_2.java Ex:Ch13_6_3.java

17 13-7 InputSteam/OutputStream檔案串流
處理二進位檔案:使用位元組串流的InputStream/OutStream類別處理檔案的讀取和寫入 int read() 讀取1個位元組 int read(byte[]) 讀取位元組陣列當讀到資料結尾時,傳回-1 int read(byte[],int,int) 讀取從第2個參數int索引值開始長度為第3個參數int的位元組陣列 void write(int) 寫入整數 void write(byte[]) 寫入1個位元組陣列 void write(byte[],int,int) 寫入從第2個參數int索引值開始長度為第3個參數int的位元組陣列

18 位元組串流要轉換成字元串流:使用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

19 13-8 隨機存取檔案的處理 Ex:Ch13_8.java
使用非順序和隨機方式來存取檔案內容,可以使用索引存取元素,這個索引稱為「檔案指標」。 RandomAccessFile input = new RandomAccessFile(file,"rw"); RandomAccessFile input = new RandomAccessFile(file,"r"); int skip Bytes(int) 移動檔案指標,向前移動參數int個位元組 void seek(int) 移動檔案指標到參數int位置,這是long參數,位置從0的檔頭開始 long getFilePointer() 取得目前檔案指標的位置,這是long整數 Ex:Ch13_8.java


Download ppt "Java 的例外與輸入出檔案處理 Jing Ming Huang."

Similar presentations


Ads by Google