1. 程式人生 > >Java進行I/O操作時正確關閉I/O流的程式碼示例

Java進行I/O操作時正確關閉I/O流的程式碼示例

一、錯誤示例1

    public void CopyFile ()
        {
            FileReader fr = null;
            FileWriter fw = null;
            try {
                fr = new FileReader("c:\\xy1.txt"); // 1.
                fw = new FileWriter("c:\\xy2.txt"); // 2.
                char[] charBuffer = new char[1024];
                int len = 0;
                while ((len = fr.read(charBuffer)) != -1) {
                    fw.write(charBuffer, 0, len);
                }
                System.out.println("檔案複製成功");
            } catch (IOException e) {
                throw new RuntimeException("檔案拷貝操作失敗");
            } finally {
                try {
                    fr.close(); // 3.
                    fw.close(); // 4.
                } catch (IOException e) {
                    throw new RuntimeException("關閉失敗");
                }
            }
        }

若1.中程式碼出錯,fr根本就沒有初始化,執行3.的時候就會報空指標異常。2.4.同樣是這個道理。

二、錯誤示例2

     public void CopyFile ()
        {
            FileReader fr = null;
            FileWriter fw = null;
            try {
                fr = new FileReader("c:\\xy1.txt"); // 1.
                fw = new FileWriter("c:\\xy2.txt"); // 2.
                char[] charBuffer = new char[1024];
                int len = 0;
                while ((len = fr.read(charBuffer)) != -1) {
                    fw.write(charBuffer, 0, len);
                }
                System.out.println("檔案複製成功");
            } catch (IOException e) {
                throw new RuntimeException("檔案拷貝操作失敗");
            } finally {
                try {
                    if (null != fr) {
                        fr.close(); // 3.
                    }
                    if (null != fw) {
                        fw.close(); // 4.
                    }
                } catch (IOException e) {
                    throw new RuntimeException("關閉失敗"); // 5.
                }
            }
        }

加上是否為空的判斷可以避免空指標異常。但是如果3.執行出錯,程式會直接進入5.而4.根本沒有得到執行,導致無法關閉。

三、正確示例(

    public void CopyFile ()
        {
            FileReader fr = null;
            FileWriter fw = null;
            try {
                fr = new FileReader("c:\\xy1.txt");
                fw = new FileWriter("c:\\xy2.txt");
                char[] charBuffer = new char[1024];
                int len = 0;
                while ((len = fr.read(charBuffer)) != -1) {
                    fw.write(charBuffer, 0, len);
                }
                System.out.println("檔案複製成功");
            } catch (IOException e) {
                throw new RuntimeException("檔案拷貝操作失敗"); //1.
            } finally {
                try {
                    if (null != fr) {
                        fr.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException("關閉失敗"); //2.
                }

                try {
                    if (null != fw) {
                        fw.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException("關閉失敗"); //3.
                }
            }
        }

博主補充:        

        上面的程式碼處理流程是沒問題的,但程式碼示例在1 2 3處有一些問題,在這裡是不能使用throw的,如果使用throw將導致異常在被catch抓取後再次丟擲,此時將會將異常拋到上層應用,導致程式中斷,輸入輸出流無法關閉。示例作者的本意應該只是表明這裡將會抓取到異常,但這樣寫的話容易給初學者造成誤導,所以在這裡補充說明一下。正確示例如下:

四、正確示例(

public void CopyFile ()
        {
            FileReader fr = null;
            FileWriter fw = null;
            try {
                fr = new FileReader("c:\\xy1.txt");
                fw = new FileWriter("c:\\xy2.txt");
                char[] charBuffer = new char[1024];
                int len = 0;
                while ((len = fr.read(charBuffer)) != -1) {
                    fw.write(charBuffer, 0, len);
                }
                System.out.println("檔案複製成功");
            } catch (IOException e) {
                //此處可以列印錯誤日誌
                e.printStackTrace();
            } finally {
                try {
                    if (null != fr) {
                        fr.close();
                    }
                } catch (IOException e) {
                    //此處可以列印錯誤日誌
                    e.printStackTrace();
                }

                try {
                    if (null != fw) {
                        fw.close();
                    }
                } catch (IOException e) {
                    //此處可以列印錯誤日誌
                    e.printStackTrace();
                }
            }
        }