我可以在FileChannel中写入任何InputStream吗?
我使用java.nio.channels.FileChannel打开一个文件并将其锁定,然后向输出文件写入一个InputStream。InputStream可以由另一个文件、URL、套接字或任何东西打开。我写了以下代码:
FileOutputStream outputStream = new FileOutputStream(outputFile);
FileChannel outputChannel = outputStream.getChannel();
FileLock lock = outputChannel.lock();
try {
outputChannel.transferFrom(???);
} finally {
lock.release();
outputChannel.close();
outputStream.close();
}但是,outputChannel.transferFrom(...)的第一个参数请求ReadableByteChannel对象。因为我使用InputStream作为输入,所以它没有inputStream.getChannel()方法来创建所需的通道。
有没有办法从InputStream获取ReadableByteChannel?
发布于 2011-07-08 10:59:24
Channels.newChannel(InputStream in)http://docs.oracle.com/javase/7/docs/api/java/nio/channels/Channels.html
发布于 2011-07-08 11:04:13
您可以使用ReadableByteChannel readableChannel = Channels.newChannel(myinputstream)。
https://stackoverflow.com/questions/6619516
复制相似问题