我使用以下方式将InputStream写入文件:
private void writeToFile(InputStream stream) throws IOException {
String filePath = "C:\\Test.jpg";
FileChannel outChannel = new FileOutputStream(filePath).getChannel();
ReadableByteChannel inChannel = Channels.newChannel(stream);
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(true) {
if(inChannel.read(buffer) == -1) {
break;
}
buffer.flip();
outChannel.write(buffer);
buffer.clear();
}
inChannel.close();
outChannel.close();
}
我想知道这是否是使用NIO的正确方法.我读了一个方法FileChannel.transferFrom,它有三个参数:
> ReadableByteChannel src
>长仓
>长计数
在我的情况下,我只有src,我没有位置和数量,有什么办法可以使用这种方法来创建文件?
还有对于Image,有没有更好的方法来创建图像只有从InputStream和NIO?
任何信息对我来说都是非常有用的.在这里有类似的问题,在SO中,但我找不到任何适合我的情况的特定解决方案.