为什么不能将 RandomAccessFile 转换为 Inputstream?
当我执行此转换时,出现编译错误:
RandomAccessFile raf = new RandomAccessFile(...)
InputStream is = (InputStream)raf;
RandomAccessFile
应该是 InputStream
的子类,尽管不是直接子类。
来自文档:
RandomAccessFile
实现了 DataInput
,它反过来 DataInputstream
& InputStream
为什么这是无效的?
另外,感谢您对使用 RandomAccessFile
作为 InputStream
的正确方法提出意见?
我正在考虑包装方法。
I get compilation error when I do this cast:
RandomAccessFile raf = new RandomAccessFile(...)
InputStream is = (InputStream)raf;
RandomAccessFile
is supposed to subclass InputStream
although not directly.
From docs:
RandomAccessFile
implements DataInput
which inturn DataInputstream
& InputStream
Why is this invalid?
Also appreciate your input on what would be the right way to use the RandomAccessFile
as InputStream
?
I am thinking of wrapper approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Channels 实用程序类可以轻松实现这一点...
This is easily achievable using the Channels utility class...
RandomAccessFile
扩展对象
,并且不扩展InputStream
。如果您想从
RandomAccessFile
获取InputStream
我认为实现包装类是您最简单的选择。幸运的是,InputStream
的唯一抽象方法是read()
。DataInputStream
是InputStream
的子类,它也恰好实现了DataInput
。继承和接口实现树如下所示:您可以在任何可以使用
InputStream
或DataInput
的地方使用DataInputStream
。您可以在任何可以使用DataInput
的地方使用RandomAccessFile
。但是您不能像这样使用强制转换在继承层次结构中向上然后向下移动。特别是,将类转换为子类(或实现的接口)将引发 ClassCastException ,除非该对象恰好是子类的实例。
即使两个类碰巧扩展了
Object
,但这并不意味着它们是可以互换的。RandomAccessFile
extendsObject
, and does not extendInputStream
.If you want get an
InputStream
from aRandomAccessFile
I think implementing a wrapper class is your simplest bet. Luckily the only abstract method ofInputStream
isread()
.DataInputStream
is a subclass ofInputStream
, which also happens to implementDataInput
. The inheritance and interface implementation tree looks like this:You could use a
DataInputStream
anywhere where you could use anInputStream
or aDataInput
. You could use aRandomAccessFile
anywhere where you could use aDataInput
.But you can't go up and then down in the inheritance hierarchy like this using casts. In particular, casting a class to a subclass (or an interface to an implementation) will raise a
ClassCastException
unless the object happens to be an instance of the child class.Even though two classes happen to extend
Object
that doesn't mean they are interchangeable.为了以@robert-christian的答案为基础,首先使用
RandomAccessFile
的主要原因是seek
到某个位置,而不是skip
ping来自FileInputStream
的字节。但那为什么还要费心使用 pre-NIO API呢?To build on @robert-christian’s answer, the main reason to use
RandomAccessFile
to begin with is toseek
to some position, rather thanskip
ping bytes from aFileInputStream
. But then why bother with pre-NIO APIs at all?不,不是。请参阅 Javadoc。
这根本不是“来自文档”。你做到了。你写的根本没有意义。
DataInput
是一个接口。DataInputStream
和InputStream
是类。接口不实现或扩展类。Javadoc 实际上说的是
RandomAccessFile
扩展了java.lang.Object
并实现了Closeable、DataInput、DataOutput
。No it isn't. See the Javadoc.
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
andInputStream
are classes. Interfaces don't implement, or extend, classes.What the Javadoc actuallys says is that
RandomAccessFile
extendsjava.lang.Object
and implementsCloseable, DataInput, DataOutput
.