Why cant a RandomAccessFile be casted to Inputstream?

后端 未结 4 2021
灰色年华
灰色年华 2021-01-12 18:03

I get compilation error when I do this cast:

RandomAccessFile raf = new RandomAccessFile(...)
InputStream is = (InputStream)raf;

Rand

4条回答
  •  悲&欢浪女
    2021-01-12 18:59

    RandomAccessFile is supposed to extends InputStream although not directly.

    No it isn't. See the Javadoc.

    From docs:

    RandomAccessFile implements DataInput which in turn DataInputstream & InputStream.

    That's not 'from the docs' at all. You made it up. What you've written doesn't even make sense. DataInput is an interface. DataInputStream and InputStream are classes. Interfaces don't implement, or extend, classes.

    What the Javadoc actuallys says is that RandomAccessFile extends java.lang.Object and implements Closeable, DataInput, DataOutput.

提交回复
热议问题