x1347638460 2018-03-29 09:19 采纳率: 25%
浏览 829
已采纳

关于Scanner的close的问题。

private Readable source;

if (source instanceof Closeable) {
try {
((Closeable)source).close();
} catch (IOException ioe) {
lastException = ioe;
}
}
source明明是Readable,会有可能实现Closeable吗?

  • 写回答

4条回答 默认 最新

  • 爱_LOVE 2018-03-29 09:33
    关注

    虽然readable接口只有read方法,但closeable接口的close方法可以关闭此流并释放与其关联的任何系统资源。Closeable:

    package java.io; import java.io.IOException; public interface Closeable { /** * Closes this stream and releases any system resources associated * with it. If the stream is already closed then invoking this * method has no effect. */ public void close() throws IOException; }
    Readable:

    package java.lang; import java.io.IOException; public interface Readable { /** * Attempts to read characters into the specified character buffer. * The buffer is used as a repository of characters as-is: the only * changes made are the results of a put operation. No flipping or * rewinding of the buffer is performed. */ public int read(java.nio.CharBuffer cb) throws IOException; }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题