2015年7月23日 星期四

Java上課練習:讀取資料檔


  • 撰寫檔案輸入輸出的程式,通常都要明確捕捉例外,因為所產生的例外編譯器會檢查
  • 開啟檔案,可能發生檔案找不到,必須明確處理 FileNotFoundException


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import java.io.FileNotFoundException;
import java.io.FileReader;

public class FileReaderTest {

    public static void main(String[] args) {
        try{
        FileReader fr=new FileReader("C:/MyJava/MyJava/AAA.txt");
        //設定檔案路徑是錯的 例外處理
        }catch(FileNotFoundException e){
            System.out.println(e.getMessage());
        }
    }
}
執行結果:

FileReader 例外處理


  • 查閱 FileReader 類別 建構子 與 read()方法 可能產生的例外
  • 檔案使用完畢,必須關閉檔案,使用 close() 方法來關閉


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {

    public static void main(String[] args) {
        try{
        FileReader fr=new FileReader("C:/MyJava/MyJava/AAA.txt");
        int i;
        while((i=fr.read()) !=-1){ //若檔案讀取進來讀完會回傳-1
            char c=(char)i;//將數字轉為字串
            System.out.println(c);//印出字串
        }
        fr.close();//當檔案讀取完畢後,將資源關閉
        }catch(FileNotFoundException e){
            System.out.println(e.getMessage());
        }catch(IOException e){
            System.out.println(e.getMessage());
        }
    }
}

當發生例外,會導致沒有辦法把資源關閉,所以要額外再做finally判斷


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {

    public static void main(String[] args) {
        FileReader fr=null;
        try {
            fr = new FileReader("C:/Users/Administrator/Desktop/MyJava/AAA.txt");
            int i;
            while ((i = fr.read()) != -1) { //若檔案讀取進來讀完會回傳-1
                char c = (char) i; //將數字轉為字串
                System.out.print(c);//印出字串
            }
           // fr.close();前面發生例外不會執行,所以將關閉的程式碼移到finally
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }finally{
            try{
                if(fr!=null){
                    fr.close();
                }
            }catch(IOException e){
                System.out.println(e);
            }
        }
    }

}

用 Java 7 新寫法,改善 finally 的 close() 的寫法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest2 {

    public static void main(String[] args) {
        //java7新寫法,資源會自動關閉
        try (FileReader fr = new FileReader("C:/Users/Administrator/Desktop/MyJava/AAA.txt")){
            int i;
            while ((i = fr.read()) != -1) { //若檔案讀取進來讀完會回傳-1
                char c = (char) i; //將數字轉為字串
                System.out.print(c);//印出字串
            }
           // fr.close();前面發生例外不會執行,所以將關閉的程式碼移到finally
        } catch (IOException e) {
            //FileNotFoundException是一種IOException
            //所以捕捉IOException等同涵蓋FileNotFoundException
            System.out.println(e.getMessage());
        } catch (Exception e) {
            //剩下只要是一種Exception,通抓
            System.out.println(e.getMessage());
        }
     }
}

沒有留言:

張貼留言