简体   繁体   English

为什么不能将RandomAccessFile转换为Inputstream?

[英]Why cant a RandomAccessFile be casted to 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. RandomAccessFile应该是InputStream子类,尽管不是直接的。

From docs: 来自docs:

RandomAccessFile implements DataInput which inturn DataInputstream & InputStream RandomAccessFile实现DataInput ,它包含DataInputstreamInputStream

Why is this invalid? 为什么这个无效?

Also appreciate your input on what would be the right way to use the RandomAccessFile as InputStream ? 还要感谢您输入使用RandomAccessFile作为InputStream的正确方法吗?

I am thinking of wrapper approach. 我在想包装方法。

This is easily achievable using the Channels utility class... 使用Channels实用程序类可以轻松实现这一点......

// STEP 1:  Create random access file read-only
RandomAccessFile raf = new RandomAccessFile("/text.txt", "r");

// STEP 2:  Use Channels to convert to InputStream
InputStream is = Channels.newInputStream(raf.getChannel());

RandomAccessFile extends Object , and does not extend InputStream . RandomAccessFile扩展了Object ,并没有扩展InputStream

If you want get an InputStream from a RandomAccessFile I think implementing a wrapper class is your simplest bet. 如果你想从RandomAccessFile获得一个InputStream ,我认为实现一个包装类是你最简单的选择。 Luckily the only abstract method of InputStream is read() . 幸运的是, InputStream的唯一抽象方法是read()

RandomAccessFile implements DataInput which inturn DataInputstream & InputStream RandomAccessFile实现DataInput,它包含DataInputstream和InputStream

DataInputStream is a subclass of InputStream , which also happens to implement DataInput . DataInputStreamInputStream的子类,它也恰好实现了DataInput The inheritance and interface implementation tree looks like this: 继承和接口实现树如下所示:

           InputStream      DataInput
               \              /   \
                \            /     \
                 \          /       \
                DataInputStream   RandomAccessFile

You could use a DataInputStream anywhere where you could use an InputStream or a DataInput . 您可以在可以使用InputStreamDataInputStream任何位置使用DataInput You could use a RandomAccessFile anywhere where you could use a DataInput . 您可以在可以使用DataInput任何位置使用RandomAccessFile

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. 特别是,将类转换为子类(或实现的接口)将引发ClassCastException除非该对象恰好是子类的实例。

Even though two classes happen to extend Object that doesn't mean they are interchangeable. 尽管两个类碰巧扩展了Object ,但并不意味着它们可以互换。

RandomAccessFile is supposed to extends InputStream although not directly. RandomAccessFile应该扩展InputStream,尽管不是直接扩展。

No it isn't. 不,不是。 See the Javadoc. 见Javadoc。

From docs: 来自docs:

RandomAccessFile implements DataInput which in turn DataInputstream & InputStream. RandomAccessFile实现了DataInput ,而DataInputstream又是DataInputstreamInputStream.

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. DataInput是一个接口。 DataInputStream and InputStream are classes. DataInputStreamInputStream是类。 Interfaces don't implement, or extend, classes. 接口不实现或扩展类。

What the Javadoc actuallys says is that RandomAccessFile extends java.lang.Object and implements Closeable, DataInput, DataOutput . Javadoc实际上说的是RandomAccessFile扩展了java.lang.Object并实现了Closeable, DataInput, DataOutput

To build on @robert-christian's answer, the main reason to use RandomAccessFile to begin with is to seek to some position, rather than skip ping bytes from a FileInputStream . 为了构建@ robert-christian的答案,使用RandomAccessFile开始的主要原因是seek某个位置,而不是从FileInputStream skip ping字节。 But then why bother with pre-NIO APIs at all? 但那么为什么要使用前NIO API呢?

try (FileChannel ch = FileChannel.open(Paths.get(…), StandardOpenOption.READ)) {
    InputStream is = Channels.newInputStream(ch.position(…));
    // …
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM