zoukankan      html  css  js  c++  java
  • Java IO异常处理方式

    public class IOException{
    
        // 获取系统默认的行分隔符
        private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    
        public static void main(String[] args){
    
            try{
                FileWriter fw = new FileWriter("test.md");
    
                fw.write("abc"+LINE_SEPARATOR+"haha");
    
            } catch (IOException){
                    System.out.println(e.toString());
            } finally{
                fw.close(); // 此时, fw 是局部变量, 只在 try 内部有效, 此时 fw 属于未定义变量
            }
        }
    }
    
    // 改进第一步:
    
    public class IOException{
    
        // 获取系统默认的行分隔符
        private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    
        public static void main(String[] args){
    
            FileWriter fw = null; // 创建流对象
            try{
                // 流对象初始化
                fw = new FileWriter("k:\test.md");
    
                fw.write("abc"+LINE_SEPARATOR+"haha");
    
            } catch (IOException){
                    System.out.println(e.toString());
            } finally{
                try{
                    fw.close();// 此处有可能出现关闭失败的异常, 所以也许要 try catch
                } catch (IOException e){
                    throw new RuntimeException("关闭失败");
                }
            }
        }
    }
    
    // 运行上述程序:
    
    public class IOException{
    
        // 获取系统默认的行分隔符
        private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    
        public static void main(String[] args){
    
            FileWriter fw = null;
            try{
                // 流对象初始化
                fw = new FileWriter("k:\test.md"); // 此处出现异常: FileNotFoundException
                                                    // 该异常是 IOException 的子类
    
                fw.write("abc"+LINE_SEPARATOR+"haha");
    
            } catch (IOException){
                    System.out.println(e.toString);
            } finally{
                try{
                    fw.close();  // 由于文件没有创建成功, 因此此处抛出 NUllPointerException
                } catch (IOException e){
                    throw new RuntimeException("关闭失败");
                }
            }
        }
    }
    
    // 改进第二步:
    
    public class IOException{
    
        // 获取系统默认的行分隔符
        private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    
        public static void main(String[] args){
    
            FileWriter fw = null;
            try{
                // 流对象初始化
                fw = new FileWriter("k:\test.md"); // 此处出现异常: FileNotFoundException
                                                    // 该异常是 IOException 的子类
    
                fw.write("abc"+LINE_SEPARATOR+"haha");
    
            } catch (IOException){
                    System.out.println(e.toString);
            } finally{
                if(fw != null) // 增加一条判断语句
                try{
                    fw.close();  
                } catch (IOException e){
                    throw new RuntimeException("关闭失败");
                }
            }
        }
    }
    
    

    参考资料:

  • 相关阅读:
    JAVA中堆和栈的区别
    怎么回答面试官:你对Spring的理解?
    如何设计一个高可用、高并发秒杀系统
    这应该是把Java内存区域讲的最清楚的一篇文章
    Spring Cloud底层原理解析
    Spring事务管理详解
    选择合适Redis数据结构,减少80%的内存占用
    最强Java并发编程详解:知识点梳理,BAT面试题等
    深入理解HashMap
    Springboot 优雅停止服务的几种方法
  • 原文地址:https://www.cnblogs.com/linkworld/p/7502213.html
Copyright © 2011-2022 走看看