Java中如何释放资源呢?

欣喜 Java经验 发布时间:2024-01-22 09:40:44 阅读数:7865 1
下文笔者讲述java中释放资源的两种方式分享,如下所示
java释放资源的方法:
     1.try catch finally中
              将释放代码写在finally中
      2.try (申明对象)  catch 
              申请对象的代码,会最终得到释放

1.try-catch-finally

package example;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

//使用 try-catch-finally 释放资源的模板
public class Example01 {
    public static void main(String[] args) {
        //声明在外面,如果在try里面声明,finally中访问不到is
        InputStream is = null;
        try {
            is = new FileInputStream("D:\\test123.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                // 在这里判断是否为空,是因为可能在 FileInputStream 实例化之前 就发生了异常
                if (is != null) is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

finally运行时刻分析

package example;

public class Example02 {
    public static void main(String[] args) {
        try {
            System.out.println(10/0);
        } catch (Exception e) {
            System.out.println("catch 执行了");
        } finally {
            System.out.println("finally 执行了");
        }
    }
}

----运行以上代码,将输出以下信息-----
    catch 执行了
    finally 执行了	
注意事项:
        发生异常时
         先运行catch
         再运行finally

当try或catch中return,finally还会运行吗?

package example;

public class Example03 {
    public static void main(String[] args) {
        System.out.println(chu(10, 0));
    }

    public static int chu(int a, int b) {
        try {
            System.out.println("try 执行了");
            return a / b;
        } catch (Exception e) {
            System.out.println("catch 执行了");
            return -1;
        } finally {
            System.out.println("finally 执行了");
            return Integer.MAX_VALUE;
        }
    }
}

----运行以上代码,将输出以下信息----
	try 执行了
	finally 执行了
	2147483647
try或cantch中使用了return
    finally也会运行
例2
package example;

public class Example04 {
    public static void main(String[] args) {
        System.out.println(chu(10, 0));
    }

    public static int chu(int a, int b) {
        try {
            System.out.println("try 执行了");
            return a / b;
        } catch (Exception e) {
            System.out.println("catch 执行了");
            return -1;
        } finally {
            System.out.println("finally 执行了");
        }
    }
}

----运行以上代码,将输出以下信息----
    try 执行了
	catch 执行了
	finally 执行了
	-1
注意事项:
      当finally中没有return
      那么在执行完finally后
       仍然会执行原来代码块中的return   

try-catch-finally的缺点:
代码量大,改进方法

try-with-resources(jdk7提供)

优化try catch finally代码量大的缺点

try(定义资源1;定义资源2;...){
	可能出现异常的代码;
}catch(){
	异常的处理代码;
}

当资源试使用结束后,会自动调用close方法
package example;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

//使用 try-with-resources 释放资源的模板
public class Example01 {
    public static void main(String[] args) {
        try (InputStream is = new FileInputStream("D:\\test123.txt")) {
            //具体操作
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
注意事项:
   try后面的小括号里只能声明资源文件
    所有资源都实现了AutoCloseable接口
      并且都用close方法
      模拟一个资源
package example;

public class Resource01 implements AutoCloseable{
    @Override
    public void close() throws Exception {
        System.out.println("释放了 Resource01 的资源");
    }
}
package example;

//使用 try-with-resources 释放资源的模板
public class Example01 {
    public static void main(String[] args) {
        try (Resource01 resource01 = new Resource01()) {
            //具体操作
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

----运行以上代码,将输出以下信息-------
    释放了 Resource01 的资源
结论:
    自动调用了close()方法关闭资源
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202401/17058876917720.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者