java abstrict class,java - implements Closeable或实现AutoCloseab

java - implements Closeable或实现AutoCloseab

我正在学习Java,我在implements Closeable和IOstream接口上找不到任何好的解释。

当我实现IOstream时,我的Eclipse IDE创建了一个方法close()。

我可以在没有界面的情况下使用IOstream关闭流。 但是,我无法理解如何使用该接口实现close()方法。 而且,这个界面的目的是什么?

另外我想知道:我如何检查IOstream是否真的关闭了?

我正在使用下面的基本代码

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 {

}

malas asked 2019-08-03T02:49:41Z

6个解决方案

157 votes

AutoCloseable(在Java 7中引入)使得可以使用try-with-resources习惯用法:

public class MyResource implements AutoCloseable {

public void close() throws Exception {

System.out.println("Closing!");

}

}

现在你可以说:

try(MyResource res = new MyResource()) {

//use res here

}

和JVM将自动为您调用AutoCloseable。 AutoCloseable是一个较旧的界面。由于某些原因为了保持向后兼容性,语言设计者决定创建一个单独的兼容性。 这不仅允许在try-with-resources中使用所有Closeable类(如抛出IOException的流),还允许从close()中抛出更多常规检查异常。

如有疑问,请使用AutoCloseable,贵班用户将不胜感激。

Tomasz Nurkiewicz answered 2019-08-03T03:31:54Z

61 votes

Closeable扩展AutoCloseable,专门用于IO流:它抛出IOException而不是Exception,并且是幂等的,而AutoCloseable不提供此保证。

这两个接口的javadoc都解释了这一点。

实现AutoCloseable(或Closeable)允许将类用作Java 7中引入的try-with-resources构造的资源,该构造允许在块的结尾自动关闭这些资源,而不必添加关闭的finally块。 资源明确。

您的类不代表可关闭的资源,并且实现此接口绝对没有意义:无法关闭IOTest。 它甚至不可能实例化它,因为它没有任何实例方法。 请记住,实现接口意味着类和接口之间存在is-a关系。 你在这里没有这样的关系。

JB Nizet answered 2019-08-03T03:34:34Z

31 votes

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.

You only have to (or should) implement pw.close() or pw.close() if you are about to implement your own pw.close(), which handles files or any other resources which needs to be closed.

如果您要实施自己的pw.close()(处理文件或需要关闭的任何其他资源),您只需(或应该)实施pw.close()或pw.close()。...

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) {

}

}

}

上面的代码与Java 6相关。 在Java 7中,这可以更优雅地完成(请参阅此答案)。

Kai answered 2019-08-03T03:29:39Z

6 votes

这是一个小例子

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 ");

}

}

这是输出:

GeneralTest

From Close - AutoCloseable

From Final Block

Lova Chittumuri answered 2019-08-03T03:35:16Z

5 votes

try-with-resources声明。

The finally is a finally statement that declares one or more resources. A try-with-resources is an object that must be closed after the program is finished with it. The BufferedReader.readLine ensures that each resource is closed at the end of the statement. Any object that implements IOException, which includes all objects which implement java.io.Closeable, can be used as a resource.

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

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 finally, in Java SE 7 and later, implements the interface finally. Because the try-with-resources 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).

在Java SE 7之前,您可以使用finally块来确保关闭资源,无论try语句是正常还是突然完成。 以下示例使用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();

}

}

请参阅文档。

inderisonline answered 2019-08-03T03:56:48Z

1 votes

最近我读了一本Java SE 8 Programmer Guide ii Book。

我发现了AutoCloseable与Closeable之间的区别。

AutoCloseable接口是在Java 7中引入的。在此之前,另一个接口存在称为可关闭。 它类似于语言设计者想要的东西以下例外:

Closeable限制抛出到IOException的异常类型。

Closeable要求实现是幂等的。

The language designers emphasize backward compatibility. Since changing the existing

interface was undesirable, they made a new one called AutoCloseable. This new

interface is less strict than Closeable. Since Closeable meets the requirements for

AutoCloseable, it started implementing AutoCloseable when the latter was introduced.

Arvind Katte answered 2019-08-03T04:03:42Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值