Java IO异常处理方式

本文介绍了使用Java进行文件操作的基本方法,包括如何利用FileWriter类写入文件,并展示了如何处理可能出现的IOException异常。此外,还详细说明了如何正确关闭资源以避免内存泄漏等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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("关闭失败");
            }
        }
    }
}

参考资料:

转载于:https://www.cnblogs.com/linkworld/p/7502213.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值