在IntelliJ IDEA中,如何使用try-with-resources包围? [英] In IntelliJ IDEA, how to surround with try-with-resources?

查看:3176
本文介绍了在IntelliJ IDEA中,如何使用try-with-resources包围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IntelliJ IDEA中,我可以按下 Surround with 快捷方式 CTRL-ALT-T 以试用一段代码/ catch块等等。

In IntelliJ IDEA, I can press the "Surround with" shortcut CTRL-ALT-T to surround a block of code with a try / catch block, among other things.

我想将资源部分包围到try-with-resources块中:

I would like to surround the resource part into a try-with-resources block:

Writer out = Files.newBufferedWriter(destination, StandardCharsets.UTF_8);
temp.process(model, out);

对此:

try (Writer out = Files.newBufferedWriter(destination, StandardCharsets.UTF_8)) {
    temp.process(model, out);
}

但是,按 CTRL时此选项不可用-ALT-T

如何使用try-with-resources块包围代码块?

How can I surround a block of code with a try-with-resources block?

推荐答案

在代表 AutoCloseable的任何表达式上按 ALT-ENTER

Press ALT-ENTER on any expression representing an AutoCloseable.

使用try-with-resources块进行环绕是意向行动。它不是 Surround with 菜单中的选项。

"Surround with try-with-resources block" is an Intention action. It is not an option available in the "Surround with" menu.

这篇关于在IntelliJ IDEA中,如何使用try-with-resources包围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆