我试过使用这段代码:
saver=new FileOutputStream(file);
byte c;
while ( content.readable() ){ //content is a ChannelBuffer type
c = content.readByte();
saver.write(c);
}
但是由于文件流是二进制的,写入速度好像真的很慢!有什么办法可以真正快速地将 ChannelBuffer 保存到文件中吗?
最佳答案
尝试将整个缓冲区写入文件。本示例代码来自netty文件上传app .
FileOutputStream outputStream = new FileOutputStream(file);
FileChannel localfileChannel = outputStream.getChannel();
ByteBuffer byteBuffer = buffer.toByteBuffer();
int written = 0;
while (written < size) {
written += localfileChannel.write(byteBuffer);
}
buffer.readerIndex(buffer.readerIndex() + written);
localfileChannel.force(false);
localfileChannel.close();
关于java - netty - 如何将 ChannelBuffer 类型保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467408/