NIO-FileChannel

NIO的用法举例

创建一个NIO的流程

  1. 创建FileChannel 通道
  2. 创建缓存Buffer,大小自己设置
  3. 读取数据到Buffer 注意此时的Buffer是写模式
  4. 切换Buffer的模式,从写到读
  5. 通过Buffer中的get方法读取到Buffer中的数据
  6. 清楚缓存关闭文件流

FileChannel的读操作例子

package NIOTest.channel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * FileChannel读取数据到buffer中
 */
public class FileChannelDemo1 {
    public static void main(String[] args) throws IOException {
        //创建FileChannel
        RandomAccessFile accessFile = new RandomAccessFile("1.txt","rw");
        FileChannel channel = accessFile.getChannel();
        //创建Buffer
        ByteBuffer buf = ByteBuffer.allocate(1024);
        //读取数据到buffer
        int read = channel.read(buf);
        while (read!=-1){
            System.out.println(read);
            //切换buffer的读写模式
            buf.flip();
            System.out.println(buf);
            while (buf.hasRemaining()){
                System.out.println((char) buf.get());
            }
            //清除缓存
            buf.clear();
            read = channel.read(buf);
        }
        accessFile.close();
    }
}

 FileChannel写操作

package NIOTest.channel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

//FileChannel写操作
public class FileChannelDemo2 {

    public static void main(String[] args) throws IOException {
        //打开一个Channel通道
        File file = new File("2.txt");
        file.createNewFile();
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        FileChannel channel = randomAccessFile.getChannel();
        String newData = "阿达顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶";
        //创建Buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //清楚buffer缓存区域
        byteBuffer.clear();
        //向buffer中写入所需要的字节流数据
        byteBuffer.put(newData.getBytes(StandardCharsets.UTF_8));
        //反转byteBuffer读写模式
        byteBuffer.flip();
        //写入通道
        while (byteBuffer.hasRemaining()){
            channel.write(byteBuffer);
        }
        channel.close();

    }
}

FileChannel基本方法 

  1. position(): 需要在FileChannel中的某个特定位置进行数据的读写操作。可以通过调用position()方法获取FileChannel的当前位置.也可以通过调用position(long pos)方法设置FileChannel的当前位置;long pos = channel.position;channel.position(pos+123);
  2. size(): 返回该实例所关联文件的大小;long filesieze  =  channel.size();
  3. truncate(): 可以截取一个文件;channel.truncate(1024)
  4. force()方法:FileChannel方法将通道里尚未写入磁盘的数据强制写入磁盘。force()方法有一个boolean类型的参数,指明是否同时将文件元数据(权限信息等)写到磁盘上。

 FileChannel两个重要方法

通道之间的数据传输:

如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel传输到另外一个channel。

  transferTo

public void transferToTest() throws IOException {

        //创建两个fileChannel
        RandomAccessFile randomAccessFile1 = new RandomAccessFile("1.txt","rw");
        FileChannel channel1 = randomAccessFile1.getChannel();


        RandomAccessFile randomAccessFile2 = new RandomAccessFile("2.txt","rw");
        FileChannel channel2 = randomAccessFile2.getChannel();

        //从channel1 数据传输到 channel2
        //    channel2.transferFrom(channel1,0,(long) channel1.size());
        channel1.transferTo(0,channel1.size(),channel2);
        channel1.close();
        channel2.close();
    }

  transferFrom

   public static void main(String[] args) throws IOException {

        //创建两个fileChannel
        RandomAccessFile randomAccessFile1 = new RandomAccessFile("1.txt","rw");
        FileChannel channel1 = randomAccessFile1.getChannel();


        RandomAccessFile randomAccessFile2 = new RandomAccessFile("2.txt","rw");
        FileChannel channel2 = randomAccessFile2.getChannel();

        //从channel1 数据传输到 channel2
        channel2.transferFrom(channel1,0,(long) channel1.size());

        channel1.close();
        channel2.close();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值