Java之文件IO

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值