zoukankan      html  css  js  c++  java
  • IO流之IO的异常处理

    如果发生了IO的异常。我们在实际开发中,对异常时如何处理的,我们来演示一下。

    public class FileOutputStreamDemo3 {
    	public static void main(String[] args) {
    		File file = new File("c:\file.txt");
    		//定义FileOutputStream的引用
    		FileOutputStream fos = null;
    		try {
    			//创建FileOutputStream对象
    			fos = new FileOutputStream(file);
    			//写出数据
    			fos.write("abcde".getBytes());
    		} catch (IOException e) {
    			System.out.println(e.toString() + "----");
    throw new RuntimeException("文件写入失败,重试");
    
    		} finally {
    			//一定要判断fos是否为null,只有不为null时,才可以关闭资源
    			if (fos != null) {
    				try {
    					fos.close();
    				} catch (IOException e) {
    					throw new RuntimeException("关闭资源失败");
    				}
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    寒假记录六
    寒假记录5
    寒假记录4
    寒假记录3
    寒假记录2
    寒假记录1
    hive数据库课堂测试
    第一周
    个人总结
    课程总结
  • 原文地址:https://www.cnblogs.com/lxx2014/p/9516797.html
Copyright © 2011-2022 走看看