Java中的FileChannel实现了ReadableByteChannel和WritableByteChannel,这使它们成为双向的.
问题:
要创建FileChannel,我需要FileInputStream(用于读取)和FileOutputStream(用于写入).不是因为我需要实例化这两个流而无法使它们成为双向的目的吗?
跟进问题:
我在很多地方看到过引用,声明java.io是面向流的,而java.nio是面向块的(这里).那他们为什么要通过InputStream和OutputStream进行实例化呢?块面向概念,只是Streams的抽象吗?
要创建FileChannel,我需要FileInputStream(用于读取)和FileOutputStream(用于写入).
你没有.您可以使用FileChannel.open(Path, OpenOption...)
Path path = ...;
FileChannel channel = FileChannel.open(path, options)
channel.read(byteBuffer);
channel.write(byteBuffer);
Run Code Online (Sandbox Code Playgroud)
看看这里的回答你的后续问题.