Java中的’defer’等效语句是什么?

huangapple go评论132阅读模式
英文:

What is the 'defer' equivalent for Java

问题

这只是一个简短的Go代码示例:

  1. package main
  2. import "fmt"
  3. func main() {
  4. defer fmt.Println("world") // 使用关键字'defer'
  5. fmt.Println("hello")
  6. }

我正在寻找Java中的'defer'的等效方式。

除了使用try/catch/finally之外,我可以使用以下方式:

  1. try {
  2. // 做一些事情
  3. } finally {
  4. // 使用'defer'的代码
  5. }

是否有其他不使用try/catch/finally的替代方法?

英文:

This is just a short example of Go code:

  1. package main
  2. import "fmt"
  3. func main() {
  4. defer fmt.Println("world") //use of keyword 'defer'
  5. fmt.Println("hello")
  6. }

I am finding an equivalent of 'defer' in Java.

Instead of 'defer' I can use

  1. try {
  2. //do something
  3. } finally {
  4. //code using defer
  5. }

Is there any alternative without using try/catch/finally?

答案1

得分: 12

Java 7引入了"try-with-resources"语句。

"try-with-resources"语句是一个try语句,用于声明一个或多个资源。资源是在程序使用完毕后必须关闭的对象。"try-with-resources"语句确保每个资源在语句结束时都会被关闭。任何实现了java.lang.AutoCloseable接口的对象都可以作为资源使用,其中包括所有实现了java.io.Closeable接口的对象。

以下示例从文件中读取第一行。它使用BufferedReader的实例从文件中读取数据。BufferedReader是一个在程序使用完毕后必须关闭的资源:

  1. static String readFirstLineFromFile(String path) throws IOException {
  2. try (BufferedReader br = new BufferedReader(new FileReader(path))) {
  3. return br.readLine();
  4. }
  5. }

在这个示例中,try-with-resources语句中声明的资源是一个BufferedReader。声明语句出现在try关键字之后的括号内。在Java SE 7及更高版本中,BufferedReader类实现了java.lang.AutoCloseable接口。因为BufferedReader实例是在try-with-resources语句中声明的,所以无论try语句是否正常完成或异常终止(例如BufferedReader.readLine方法抛出IOException),它都会被关闭。

英文:

Java 7 has a try-with-resources statement.

> The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
>
> The following example reads the first line from a file. It uses an
> instance of BufferedReader to read data from the file. BufferedReader
> is a resource that must be closed after the program is finished with
> it:
>
> static String readFirstLineFromFile(String path) throws IOException {
> try (BufferedReader br = new BufferedReader(new FileReader(path))) {
> return br.readLine();
> }
> }
>
> In this example, the resource declared in the try-with-resources
> statement is a BufferedReader. The declaration statement appears
> within parentheses immediately after the try keyword. The class
> BufferedReader, in Java SE 7 and later, implements the interface
> java.lang.AutoCloseable. Because the BufferedReader instance is
> declared in a try-with-resource statement, it will be closed
> regardless of whether the try statement completes normally or abruptly
> (as a result of the method BufferedReader.readLine throwing an
> IOException).

答案2

得分: 4

在Java 7及以上版本中,你可以使用try-with-resource语句:

  1. public static String readFirstLineFromFile(String path) throws IOException {
  2. try (BufferedReader br = new BufferedReader(new FileReader(path))) {
  3. return br.readLine();
  4. }
  5. }

当退出try块时,它会自动关闭资源。

文档:链接

英文:

In java 7 and above you can use try-with-resource:

  1. public static String readFirstLineFromFile(String path) throws IOException {
  2. try (BufferedReader br = new BufferedReader(new FileReader(path))) {
  3. return br.readLine();
  4. }
  5. }

when you exit the try it will close the resource
docs: link

答案3

得分: 0

你也可以使用一些额外的库来实现类似的功能。不幸的是,需要处理 IOException 异常。

  1. import com.google.common.io.Closer;
  2. import lombok.Cleanup;
  3. import lombok.SneakyThrows;
  4. import java.io.IOException;
  5. public class Main {
  6. @SneakyThrows(IOException.class)
  7. public static void main(String[] args) {
  8. @Cleanup Closer defer = Closer.create();
  9. defer.register(() -> System.out.println("world"));
  10. System.out.println("hello");
  11. }
  12. }

如果你不喜欢处理异常,并且觉得值得的话,你可以像这样进行封装:

  1. import com.google.common.io.Closer;
  2. import lombok.Cleanup;
  3. import lombok.SneakyThrows;
  4. import java.io.Closeable;
  5. import java.io.IOException;
  6. public class Main {
  7. public static void main(String[] args) {
  8. @Cleanup Defer defer = Defer.create();
  9. defer.register(() -> System.out.println("world"));
  10. System.out.println("hello");
  11. }
  12. }
  13. // 在某个工具包中
  14. class Defer implements Closeable {
  15. private final Closer closer;
  16. private Defer(Closer closer) {
  17. this.closer = closer;
  18. }
  19. public static Defer create() {
  20. return new Defer(Closer.create());
  21. }
  22. public <C extends Closeable> C register(C closeable) {
  23. return this.closer.register(closeable);
  24. }
  25. @SneakyThrows(IOException.class)
  26. @Override
  27. public void close() {
  28. this.closer.close();
  29. }
  30. }
英文:

You could also do something like this with some extra libraries. Unfortunately, there is the IOException to deal with.

  1. import com.google.common.io.Closer;
  2. import lombok.Cleanup;
  3. import lombok.SneakyThrows;
  4. import java.io.IOException;
  5. public class Main {
  6. @SneakyThrows(IOException.class)
  7. public static void main(String[] args) {
  8. @Cleanup Closer defer = Closer.create();
  9. defer.register(() -&gt; System.out.println(&quot;world&quot;));
  10. System.out.println(&quot;hello&quot;);
  11. }
  12. }

If you don't like dealing with the exception, and if it's worth it for you, you could wrap it like this

  1. import com.google.common.io.Closer;
  2. import lombok.Cleanup;
  3. import lombok.SneakyThrows;
  4. import java.io.Closeable;
  5. import java.io.IOException;
  6. public class Main {
  7. public static void main(String[] args) {
  8. @Cleanup Defer defer = Defer.create();
  9. defer.register(() -&gt; System.out.println(&quot;world&quot;));
  10. System.out.println(&quot;hello&quot;);
  11. }
  12. }
  13. // in some util package
  14. class Defer implements Closeable {
  15. private final Closer closer;
  16. private Defer(Closer closer) {
  17. this.closer = closer;
  18. }
  19. public static Defer create() {
  20. return new Defer(Closer.create());
  21. }
  22. public &lt;C extends Closeable&gt; C register(C closeable) {
  23. return this.closer.register(closeable);
  24. }
  25. @SneakyThrows(IOException.class)
  26. @Override
  27. public void close() {
  28. this.closer.close();
  29. }
  30. }

huangapple
  • 本文由 发表于 2015年4月22日 13:16:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/29788307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定