
在java中使用资源时,例如文件、数据库连接、套接字等,一旦不再需要,正确关闭它们非常重要,否则可能会出现内存泄漏或资源崩溃等问题。
在java中,关闭资源的传统方法是使用try-catch-finally块,其中在finally块中,只要它们有close()方法,资源就会被关闭,有关该块的更多信息,您可以请参阅下一篇文章。
当处理可自动关闭的资源时,try-with-resources 块是 try-catch-finally 块的替代方案,对于它们实现 autocloseable 或 closeable 接口是必要的,这是自动关闭的唯一条件该块中可以使用某些资源。而无论执行流程中是否抛出异常,资源都会自动关闭。
try-with-resources 块的语法如下:
try (resourcetype resource = new resourcetype()) {
// código que utiliza el recurso
} catch (exception e) {
// manejo de excepciones
}
为了举例说明 try-with-resources 块的使用,我们假设有一个 readfile 方法,该方法接收文件名作为参数并返回包含文件内容的字符串,在第一个实例中它仅使用 try-block catch 来读取文件并手动关闭资源。
public static string readfile(string name) {
stringbuilder sb = new stringbuilder();
file file = new file(name);
try {
reader r = new filereader(file);
bufferedreader br = new bufferedreader(r);
string line = "";
while ((line = br.readline()) != null) {
sb.append(line).append("\n");
}
r.close();
br.close();
} catch (filenotfoundexception e) {
system.out.println(e.getmessage());
throw new runtimeexception(e);
} catch (ioexception e) {
system.out.println(e.getmessage());
throw new runtimeexception(e);
}
return sb.tostring();
}
现在让我们使用 try-catch-finally 块来实现此方法:
public static string readfile(string name) throws ioexception {
stringbuilder sb = new stringbuilder();
file file = new file(name);
reader r = null;
bufferedreader br = null;
try {
r = new filereader(file);
br = new bufferedreader(r);
string line = "";
while ((line = br.readline()) != null) {
sb.append(line).append("\n");
}
} catch (filenotfoundexception e) {
system.out.println(e.getmessage());
throw new runtimeexception(e);
} catch (ioexception e) {
system.out.println(e.getmessage());
throw new runtimeexception(e);
} finally {
r.close();
br.close();
}
return sb.tostring();
}
考虑这个方法,我们可以看到以下内容:
1、演示:以截图为准(已测试)2、本源码为phpweb整站打包3、作者主页:http://www.juchake.com4、关于程序安装: 本源码均为作者亲自测试过,可以正常使用,无限制,为了大家的方便安装,作者还特意录制了安装视频教程,不懂的可以学习教程后安装,谢谢,多多支持。由于是视频教程不好上传,大家到百度云下载即可http://pan.baidu.com/share/link?shar
0
立即学习“Java免费学习笔记(深入)”;
通过使用 try-with-resources 块,上面的代码被简化并且更具可读性,因为资源会自动关闭,并且不需要嵌套额外的 try-catch 块。
public static string readfile(string name) {
stringbuilder sb = new stringbuilder();
file file = new file(name);
try (reader r = new filereader(file);
bufferedreader br = new bufferedreader(r)) {
string line = "";
while ((line = br.readline()) != null) {
sb.append(line).append("\n");
}
} catch (ioexception e) {
system.out.println(e.getmessage());
throw new runtimeexception(e);
}
return sb.tostring();
}
reader 和 bufferedreader 资源在 try 块内声明和初始化,如果只是一个资源则无需放置;位于声明末尾,否则必须用 ; 分隔如示例所示。重要的是要记住,每个资源都必须实现 autocloseable 或 closeable 接口才能在此块中使用。
现在我们可以执行该方法并通过控制台打印文件内容:
var output = readFile("file.txt");
System.out.println(output);
// Output:
Hello world!
这样我们就可以确保所有使用的资源都自动关闭,而不需要显式调用 close() 方法。如果使用 try-catch-finally 块来关闭资源,ide 本身会建议在可能的情况下将其替换为 try-with-resources 块。
以上就是尝试使用 Java 资源的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号