zoukankan      html  css  js  c++  java
  • J01-Java IO流总结一 《异常捕获》

    下面演示java中处理I/O操作时的异常的正确方式,总结自书籍,书名忘啦~

    先看一种不正确的方式

    方式一:

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class Test3 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            byte[] buffer = new byte[1024];
            int temp = 0;
            
            //打开文件
            try {
                fis = new FileInputStream("e:/test_file/a.txt");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            
            //访问文件
            try {
                while(-1 != (temp = fis.read(buffer))) {
                    System.out.print(new String(buffer, 0, temp));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            //关闭文件
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }

     注意到前面的代码在读取文件的try代码块完成后关闭了文件流,虽然这种方法在某些情况下有用,但是Java提供了一种在通常情况下更好的方法。即,在finally代码块中调用close()在这种方法中,访问文件的所有方法都包含在一个try代码块中,finally代码块用来关闭文件这样,无论try代码块如何终止,文件都会被关闭。使用前面的示例,下面的方式二演示了如何重新编码读取文件的try代码块

    方式二:

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class Test4 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            byte[] buffer = new byte[1024];
            int temp = 0;
            
            //打开文件
            try {
                fis = new FileInputStream("e:/test_file/a.txt");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            
            //访问文件
            try {
                while(-1 != (temp = fis.read(buffer))) {
                    System.out.print(new String(buffer, 0, temp));
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //关闭文件
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

     这种方法的优点之一是,如果访问文件的代码由于某种与I/O无关的异常而终止,finally代码块仍然会关闭文件。虽然在这个例子中这不是一个问题,因为在发生未预料到的异常时程序简单地结束了,但是在大型的程序中却可能造成很多麻烦。使用finally可以避免这些麻烦

    有时,将程序中打开文件和访问文件的部分放到一个try代码块,然后使用一个finally代码块关闭文件,这样更加简单。例如,下面是另一种编写方式

    方式三(推荐方式):

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class Test4 {
        public static void main(String[] args) {
            FileInputStream fis = null; //先初始化为null
            byte[] buffer = new byte[1024];
            int temp = 0;
            
            try {
                //打开文件
                fis = new FileInputStream("e:/test_file/a.txt");
                
                //访问文件
                while(-1 != (temp = fis.read(buffer))) {
                    System.out.print(new String(buffer, 0, temp));
                }
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //关闭文件
                try {
                    if(null != fis) {   //非空判断
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

     在这种方法中,注意fis被初始化为null。然后,在finally代码块中,只有fis不为null时才关闭文件。可以这么做的原因是,只有文件被成功打开,fis才不为null。因为,如果在打开文件的过程中出现异常,就不应调用close()方法,否则会报NullPointerException。推荐使用这种方式!

     前面示例中的try/catch序列还可以更加精简。因为FileNotFoundException是IOException的一个子类,所以不需要单独捕获。例如,这个catch语句可以用来捕获两个异常,从而不必单独捕获FileNotFoundException。这种情况下,将显示描述错误的标准异常信息。

    方式四:

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class Test4 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            byte[] buffer = new byte[1024];
            int temp = 0;
            
            try {
                //打开文件
                fis = new FileInputStream("e:/test_file/a.txt");
                
                //访问文件
                while(-1 != (temp = fis.read(buffer))) {
                    System.out.print(new String(buffer, 0, temp));
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //关闭文件
                try {
                    if(null != fis) {   //非空判断
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    在这种方法中,任何错误,包括打开文件时发生错误,都会被一个catch语句处理。这种方法十分简洁。但要注意,如果想单独处理打开文件时发生的错误(例如,用户错误地键入了文件名),这种方法就不合适了。

    最后放一张图

  • 相关阅读:
    linux查看日志文件内容命令tail、cat、tac、head、echo
    改变自己的128种方法,教你变得更优秀!
    php 23种设计模式
    Swoole消息推送
    PHP 出现 502 解决方案
    【centos7】添加开机启动服务/脚本
    curl 参数配置详解
    i系列标准-互联网周刊
    MySQL 设计与开发规范2
    Cocos Creator webviw网页置顶增加返回键的方法
  • 原文地址:https://www.cnblogs.com/suhaha/p/9651214.html
Copyright © 2011-2022 走看看