[英]implements Closeable or implements AutoCloseable
I'm in the process of learning Java and I cannot find any good explanation on the implements Closeable
and the implements AutoCloseable
interfaces.我正在学习 Java,我找不到关于
implements Closeable
和implements AutoCloseable
接口的任何好的解释。
When I implemented an interface Closeable
, my Eclipse IDE created a method public void close() throws IOException
.当我实现一个
interface Closeable
,我的 Eclipse IDE 创建了一个方法public void close() throws IOException
。
I can close the stream using pw.close();
我可以使用
pw.close();
关闭流pw.close();
without the interface.没有接口。 But, I cannot understand how I can implement the
close()
method using the interface.但是,我无法理解如何使用该接口实现
close()
方法。 And, what is the purpose of this interface?而且,这个接口的目的是什么?
Also I would like to know: how can I check if IOstream
was really closed?我也想知道:如何检查
IOstream
是否真的关闭了?
I was using the basic code below我正在使用下面的基本代码
import java.io.*;
public class IOtest implements AutoCloseable {
public static void main(String[] args) throws IOException {
File file = new File("C:\\test.txt");
PrintWriter pw = new PrintWriter(file);
System.out.println("file has been created");
pw.println("file has been created");
}
@Override
public void close() throws IOException {
}
AutoCloseable
(introduced in Java 7) makes it possible to use the try-with-resources idiom: AutoCloseable
(在 Java 7 中引入)使得使用try-with-resources习语成为可能:
public class MyResource implements AutoCloseable {
public void close() throws Exception {
System.out.println("Closing!");
}
}
Now you can say:现在你可以说:
try (MyResource res = new MyResource()) {
// use resource here
}
and JVM will call close()
automatically for you. JVM 会自动为你调用
close()
。
Closeable
is an older interface. Closeable
是一个较旧的界面。 For some reasonTo preserve backward compatibility, language designers decided to create a separate one.因为某些原因
为了保持向后兼容性,语言设计者决定创建一个单独的。 This allows not only all
Closeable
classes (like streams throwing IOException
) to be used in try-with-resources, but also allows throwing more general checked exceptions from close()
.这不仅允许在 try-with-resources 中使用所有
Closeable
类(如抛出IOException
流),还允许从close()
抛出更一般的检查异常。
When in doubt, use AutoCloseable
, users of your class will be grateful.如有疑问,请使用
AutoCloseable
,您班级的用户将不胜感激。
Closeable
extends AutoCloseable
, and is specifically dedicated to IO streams: it throws IOException
instead of Exception
, and is idempotent, whereas AutoCloseable
doesn't provide this guarantee. Closeable
扩展了AutoCloseable
,并且专门用于 IO 流:它抛出IOException
而不是Exception
,并且是幂等的,而AutoCloseable
不提供这种保证。
This is all explained in the javadoc of both interfaces.这在两个接口的 javadoc 中都有解释。
Implementing AutoCloseable
(or Closeable
) allows a class to be used as a resource of the try-with-resources construct introduced in Java 7, which allows closing such resources automatically at the end of a block, without having to add a finally
block which closes the resource explicitly.实现
AutoCloseable
(或Closeable
)允许将类用作 Java 7 中引入的try-with-resources构造的资源,这允许在块的末尾自动关闭此类资源,而无需添加用于关闭的finally
块资源明确。
Your class doesn't represent a closeable resource, and there's absolutely no point in implementing this interface: an IOTest
can't be closed.你的类不代表一个可关闭的资源,实现这个接口绝对没有意义:一个
IOTest
不能被关闭。 It shouldn't even be possible to instantiate it, since it doesn't have any instance method.甚至不应该实例化它,因为它没有任何实例方法。 Remember that implementing an interface means that there is a is-a relationship between the class and the interface.
请记住,实现接口意味着类和接口之间存在 is -a关系。 You have no such relationship here.
你在这里没有这种关系。
It seems to me that you are not very familiar with interfaces.在我看来,您对接口不是很熟悉。 In the code you have posted, you don't need to implement
AutoCloseable
.在您发布的代码中,您不需要实现
AutoCloseable
。
You only have to (or should) implement Closeable
or AutoCloseable
if you are about to implement your own PrintWriter
, which handles files or any other resources which needs to be closed.如果您要实现自己的
PrintWriter
,则只需(或应该)实现Closeable
或AutoCloseable
,它处理需要关闭的文件或任何其他资源。
In your implementation, it is enough to call pw.close()
.在您的实现中,调用
pw.close()
就足够了。 You should do this in a finally block:您应该在 finally 块中执行此操作:
PrintWriter pw = null;
try {
File file = new File("C:\\test.txt");
pw = new PrintWriter(file);
} catch (IOException e) {
System.out.println("bad things happen");
} finally {
if (pw != null) {
try {
pw.close();
} catch (IOException e) {
}
}
}
The code above is Java 6 related.上面的代码是 Java 6 相关的。 In Java 7 this can be done more elegantly (see this answer ).
在 Java 7 中,这可以更优雅地完成(请参阅此答案)。
Here is the small example这是一个小例子
public class TryWithResource {
public static void main(String[] args) {
try (TestMe r = new TestMe()) {
r.generalTest();
} catch(Exception e) {
System.out.println("From Exception Block");
} finally {
System.out.println("From Final Block");
}
}
}
public class TestMe implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println(" From Close - AutoCloseable ");
}
public void generalTest() {
System.out.println(" GeneralTest ");
}
}
Here is the output:这是输出:
GeneralTest
From Close - AutoCloseable
From Final Block
Recently I have read a Java SE 8 Programmer Guide ii Book.最近我读了一本 Java SE 8 Programmer Guide ii Book。
I found something about the difference between AutoCloseable
vs Closeable
.我发现了
AutoCloseable
与Closeable
之间的区别。
The AutoCloseable
interface was introduced in Java 7. Before that, another interface existed called Closeable
. AutoCloseable
接口是在 Java 7 中引入的。在此之前,存在另一个名为Closeable
接口。 It was similar to what the language designers wanted, with the following exceptions:它类似于语言设计者想要的,但有以下例外:
Closeable
restricts the type of exception thrown to IOException
. Closeable
将抛出的异常类型限制为IOException
。Closeable
requires implementations to be idempotent. Closeable
要求实现是幂等的。 The language designers emphasize backward compatibility.语言设计者强调向后兼容性。 Since changing the existing interface was undesirable, they made a new one called
AutoCloseable
.由于更改现有界面是不可取的,因此他们创建了一个名为
AutoCloseable
的新界面。 This new interface is less strict than Closeable
.这个新接口没有
Closeable
严格。 Since Closeable
meets the requirements for AutoCloseable
, it started implementing AutoCloseable
when the latter was introduced.由于
Closeable
满足AutoCloseable
的要求,因此在引入AutoCloseable
时它开始实现AutoCloseable
。
The try-with-resources
Statement. try-with-resources
声明。
The try-with-resources statement
is a try
statement that declares one or more resources. try-with-resources statement
是一种声明一个或多个资源的try
语句。 A resource
is an object that must be closed after the program is finished with it. resource
是程序完成后必须关闭的对象。 The try-with-resources statement
ensures that each resource is closed at the end of the statement. try-with-resources statement
确保每个资源在语句结束时关闭。 Any object that implements java.lang.AutoCloseable
, which includes all objects which implement java.io.Closeable
, can be used as a resource.任何实现
java.lang.AutoCloseable
对象,包括实现java.io.Closeable
所有对象,都可以用作资源。
The following example reads the first line from a file.以下示例从文件中读取第一行。 It uses an instance of
BufferedReader
to read data from the file.它使用
BufferedReader
一个实例从文件中读取数据。 BufferedReader
is a resource that must be closed after the program is finished with it: BufferedReader
是程序完成后必须关闭的资源:
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.在这个例子中,在 try-with-resources 语句中声明的资源是一个 BufferedReader。 The declaration statement appears within parentheses immediately after the try keyword.
声明语句紧跟在 try 关键字之后的括号内。 The class
BufferedReader
, in Java SE 7 and later, implements the interface java.lang.AutoCloseable
. Java SE 7 及更高版本中的
BufferedReader
类实现了接口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
).因为
BufferedReader
实例是在 try-with-resource 语句中声明的,所以无论 try 语句是正常完成还是突然完成(作为BufferedReader.readLine
方法抛出IOException
),它都会被关闭。
Prior to Java SE 7, you can use a finally
block to ensure that a resource is closed regardless of whether the try statement completes normally or abruptly.在 Java SE 7 之前,您可以使用
finally
块来确保资源关闭,无论 try 语句是正常完成还是突然完成。 The following example uses a finally
block instead of a try-with-resources
statement:以下示例使用
finally
块而不是try-with-resources
语句:
static String readFirstLineFromFileWithFinallyBlock(String path)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}
Please refer to the docs .请参阅文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.