Eclipse 支持 try-with-resource,有点像这样:
try(Outputstream resource = new FileOutputStream(file)){
// do something...
}
Run Code Online (Sandbox Code Playgroud)
自从这个功能添加到 Eclipse 以来已经有很多年了,但是还没有模板“try-with-reousource”。只存在一种“try-catch”。
我尝试制作模板,例如try($type{} ${localVar} = new $type{}){ {$cursor{} },但没有用。(还建议使用非 AutoClosable 类型)
有没有有用的资源尝试模板?
我正在使用方便的 try-with-resources 语句来关闭连接。这在大多数情况下都很有效,但只有在一种非常简单的方法中它才能正常工作。即,这里:
public boolean testConnection(SapConnection connection) {
SapConnect connect = createConnection(connection);
try ( SapApi sapApi = connect.connect() ) {
return ( sapApi != null );
} catch (JCoException e) {
throw new UncheckedConnectionException("...", e);
}
}
Run Code Online (Sandbox Code Playgroud)
sapApi 对象为非空,该方法返回 true,但从未调用 sapApi 的 close() 方法。我现在求助于使用一个工作正常的 finally 块。但这很令人费解。Java 字节码还包含对 close 的调用。有没有人见过这种行为?
编辑以澄清情况:
这是 SapApi,当然,它实现了 AutoCloseable。
class SapApi implements AutoCloseable {
@Override
public void close() throws JCoException {
connection.close(); // this line is not hit when leaving testConnection(..)
}
..
}
Run Code Online (Sandbox Code Playgroud)
下面是与 …
我的团队有相当多的代码。最近我发现了一些没有正确关闭的对象。
如何找到所有未关闭或不在try-with-resources块内的实例?
一些对象,例如Statement,ResultSet甚至没有显示警告消息。
是否有用于显示所有这些事件的扩展工具?
我正在使用 Eclipse。
AFAIK,标准try-with-resources形式
try(InputStream is= new ...){
... some reading from is
} catch (..){
... catching real problems
}
catch (IOException e) {
... if closing failed, do nothing - this clause is demanded by syntax
}
Run Code Online (Sandbox Code Playgroud)
相当于:
try{
InputStream is= new ...
... some reading from is
} catch (..){
... catching real problems
} finally {
try{
is.close();
} catch (IOException e) {
... if closing failed, do nothing
}
}
Run Code Online (Sandbox Code Playgroud)
当然,第一个变体更简单。但是我看到第二个变体绝对可以的情况,而第一个变体变得无法理解。
想象一下这种情况,当你得到代码时,try(){}出现在函数 withthrows …
这是我的代码片段:
public static void main(String[] args) {
Try.of(Main::getLines)
.onFailure(cause -> log.error("An error has occurred while parsing the file", cause));
}
private static List<Fiche> getLines() {
return Files.lines(Paths.get("insurance_sample.csv"))
.skip(1)
.map(Main::toPojo)
.filter(fiche -> fiche.getPointLongitude().equals(-81.711777))
.peek(fiche -> log.info("Fiche added with success {}", fiche))
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 try-with-resources 或在“finally”子句中关闭此“Stream”。在这条线上Files.lines(Paths.get("insurance_sample.csv"))
任何人都可以帮助我使用 Vavr 使用 try-with-resources 吗?
我有一个涉及多个类的 Java 11 项目。在当前场景中,我的 2 个类(A 和 B)实现了 java Finalize() 方法,该方法现已永久弃用。我知道该方法可能不会在不久的将来被删除,但我认为最好立即找到 Finalize 的替代方法。
A类中的finalize()主要关注于销毁一个受保护的成员变量long类型的对象,并将某些消息打印到日志中。B 类中的 Finalize() 只是将某些消息打印到日志中。
类 A 的实例是从其他几个类创建的,类 B 扩展了另一个类 ClassLoader。(下面包含代码片段。)
我经历了很多建议,例如,
这些一开始就没有得到很好的解释,即使解释得很好,这些示例也特定于单类项目,并且主方法存在于同一类中。我无法继续使用我在网上找到的最小解决方案。
经过我的研究,带有 try-with-resources 的 Autocloseable 似乎是我的最佳选择。我知道我的类 A 和 B 应该实现 Autocloseable,而被调用者(这里有点不确定)应该使用 try-with-resources。
我将不胜感激任何有助于简化这个问题的帮助,即使它是为了填补我对这个场景的理解中可能存在的空白。
A.java
class A
{
protected long a_var;
protected A(String stmt, boolean isd)
{
// a_var is initialized here
}
public void finalize()
{
if(a_var != 0)
{
log("CALL destroy !");
destroy(a_var);
log("DONE destroy !");
}
} …Run Code Online (Sandbox Code Playgroud) 我正在使用 android ,它在 android 应用程序中MediaMetaDataRetriever实现。AutoCloseable我有下面的代码
try (final MediaMetadataRetriever retriever = new MediaMetadataRetriever()) {\n retriever.setDataSource(videoUri.getPath());\n return retriever.getFrameAtTime(10, getFrameOption());\n}\nRun Code Online (Sandbox Code Playgroud)\n最小SDK > 21
\n但我遇到了以下崩溃
\nNo virtual method close()V in class Landroid/media/MediaMetadataRetriever; or its super classes (declaration of \xe2\x80\x98android.media.MediaMetadataRetriever\xe2\x80\x99 appears in /system/framework/framework.jar
如果MediaMetadataRetriever implements AutoCloseable
我正在尝试编写一些具有以下基本形式的简单套接字代码 -
try(BufferedReader request = new BufferedReader(new InputStreamReader(sock.getInputStream()))){
//Do some work...
}
//BufferedReader gets closed, but also makes the socket close
...
...
response.write(blah);//Causes exception because socket is closed
Run Code Online (Sandbox Code Playgroud)
我的问题是套接字正在关闭,但我不认为它应该是.try-with-resources创建一个BufferedReader,然后在我离开try块时自动关闭它,但由于某种原因它也会关闭整个套接字!因此,当我稍后使用相同的套接字获得我的响应代码时,我得到一个异常.有没有什么办法解决这一问题?或者我只是不必使用try-with-resources(这将不太理想)?
public class SampleCloseable implements AutoCloseable {
private String name;
public SampleCloseable(String name){
this.name = name;
}
@Override
public void close() throws Exception {
System.out.println("closing: " + this.name);
}
}
Run Code Online (Sandbox Code Playgroud)
和主要的课程
public class Main{
public static void main(String args[]) {
try(SampleCloseable sampleCloseable = new SampleCloseable("test1")){
System.out.println("im in a try block");
} catch (IOException e) {
System.out.println("IOException is never thrown");
} catch (Exception e) {
} finally{
System.out.println("finally");
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我在SampleCloseable中的close()方法中删除throws异常时,我收到编译器错误,指出IOException永远不会在相应的try块中抛出.
从Java 7开始,我们可以使用try资源:
try (One one = new One(); Two two = new Two()) {
System.out.println("try");
} catch (Exception ex) { ... }
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,为什么我必须在try-statement中创建对象?为什么我不允许在语句之前创建对象,如下所示:
One one = new One();
try (one; Two two = new Two()) {
System.out.println("try");
} catch (Exception ex) { ... }
Run Code Online (Sandbox Code Playgroud)
我没有看到任何理由,为什么这应该是一个问题.虽然我收到错误消息"此语言级别不支持资源引用".我将我的IDE(IntelliJ IDEA)设置为Java 8,因此应该可以工作.是否有充分的理由,不被允许?