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

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法