FileInputStream\FileOutputStream
字节流转缓冲流,以提高速度
BufferedOutputStreamout = newBufferedOutputStream(new FileOutputStream(file));
BufferedInputStream in = newBufferedInputStream(new FileInputStream(file));
RandomAccessFile
RandomAccessFile raf = newRandomAccessFile(videoFile.toString(), "rw");
getFilePointer
seek
FileChannel
用缓冲器操纵数据
如果想把一个字节数组写到文件中去,那么就应该使用ByteBuffer.wrap()方法把字节数组包装起来,然后用getChannel()方法在FileOutputStream上打开一个通道,接着将来自于ByteBuffer的数据写到FileChannel中.
内存映射文件
MappedByteBuffer mbb = newRandomAccessFile(videoFile.toString(), "rw").getChannel()
.map(FileChannel.MapMode.READ_WRITE,0, MediaFileLength);
缺点:文件长度无法调整
public voidunmapMmaped(ByteBuffer buffer) {
if (buffer instanceofsun.nio.ch.DirectBuffer) {
sun.misc.Cleaner cleaner =((sun.nio.ch.DirectBuffer) buffer).cleaner();
cleaner.clean();
}
}
FileChannel::truncate