Java 的例外與輸入出檔案處理

Download Report

Transcript Java 的例外與輸入出檔案處理

Java 的例外與輸入出檔案處理
Jing Ming Huang
Java的例外處理

例外(Exception):程式執行時,發生不正常執行狀態時所發生的事
件

架構:
MethodC()方法
產生錯誤的例外A
找尋例外處理
MethodB()方法
MethodA()方法
main()方法
自行丟出例外B到main
擁有例外處理A
擁有例外處理B
13-1-2 Throwable類別

Throwable類別擁有兩個直接的子類別:
Error類別:屬於JVM的嚴重錯誤,將導致程式的終止
執行,並沒有辦法使用例外處理來處理錯誤
Exception類別:其子類別是各種例外物件,也是例外
處理可以處理的部份
ArithmeticException
數學運算時產生的例外
ArrayIndexOutOfBoundsEx
ception
陣列索引值小於0或超過陣列邊界所產生的
例外
IllegalArgumentException
儲存陣列元素時型態不符產生的例外
NullPointerException
物件值為null產生的例外
例外處理的程式敘述

敘述分為三個程式區塊:try、 catch、 finally,可以建立程式的例
外處理
try { ………………} 檢查是否發生例外
catch(ExceptionType e)
參數e為例外類型的物件
{ //例外處理
……………
}
finally{……………..}一個程式選項,區塊進行程式的善後
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類別


方法本身也可以自行丟出例外,或當例外產生時,丟給其他的方法
繼承自Throwable類別的物件
13-3-1 使用throw指令丟出例外
語法: throw ThrowableObject;
ex:丟出ArithmeticException的例外物件
throw new ArithmeticException("值小於5");
使用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子類別,可以從標準輸
入讀取資料和輸出到標準輸出
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-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
13-5-3 標準輸入

從鍵盤輸入資料:開System.in標準輸出的Input StreamReader串
流,然後使用緩衝區Bufferedreader串流加速資料的處理
BufferedReader input = new BufferedReader(
new InputStreamReader(System.in));
String readLine()
Ex:Ch13_5_3.java
讀取1行「 \r 」、 「 \n 」 、
「 \r\n 」結尾的文字
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));
Ex:Ch13_6_2.java

13-6-3 檔案複製
整合前兩節範例就可以建立檔案複製程式
Ex:Ch13_6_3.java
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的
位元組陣列
位元組串流要轉換成字元串流:使用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");
int skip Bytes(int)
移動檔案指標,向前移動參數int個位元組
void seek(int)
移動檔案指標到參數int位置,這是long參數,位
置從0的檔頭開始
long getFilePointer()
取得目前檔案指標的位置,這是long整數
Ex:Ch13_8.java