NIO-Channels之FileChannel

文件通道可供多个并发线程使用。通过调用此类定义的一种打开方法来创建文件通道。 也可以通过调用该对象的getChannel方法从现有的FileInputStream,FileOutputStream或RandomAccessFile对象获取文件通道,该方法返回连接到同一基础文件的文件通道。任何更改都将更改原始对象。

下面给一个简单的应用实例,附加一些简单的注解,每个方法更详细的解释可以自行查阅JAVA API

        //创建一个RandomAccessFile(随机访问文件)对象
	RandomAccessFile raf=new RandomAccessFile("f.txt", "rw");

       //通过RandomAccessFile对象的getChannel()方法创建文件通道。FileChannel是抽象类。
        FileChannel inChannel=raf.getChannel();

        //创建一个读数据缓冲区对象
        ByteBuffer buf=ByteBuffer.allocate(48); //从本实例来看,此处缓冲区过大
        //从通道中读取数据
        int bytesRead = inChannel.read(buf);

        //创建一个写数据缓冲区对象
        ByteBuffer buf2=ByteBuffer.allocate(48);
        buf2.put("filechannel test\n".getBytes());//写入数据
        buf2.flip();//转换成读模式,此方法只是改变文件指针的数值
        raf.seek(0);//很重要,如果你想重写文件,就从开头写起,否则默认写入尾部
        inChannel.write(buf2);//把缓冲区数据写入通道,再通过通道写入文件
        buf2.clear();

        while (bytesRead != -1) {
            buf.flip();

           //如果还有未读内容,就把通道读到的内容写入文件
            while (buf.hasRemaining()) {
                inChannel.write(buf);
            }
            //清除此缓冲区(实际上并没有擦除缓冲区中的数据),位置设置为零,限制设置为容量,标记将被丢弃
            buf.clear();
            bytesRead = inChannel.read(buf);//若缓冲区小而数据多,可再次读入
        }
        //关闭RandomAccessFile对象
        inChannel.close();
        raf.close();

FileChannel类型也可以直接把一个channel通道的数据传输到另一个channel通道中

transferFrom() :transferFrom方法把数据从通道源传输到FileChannel
transferTo() :transferTo方法把FileChannel数据传输到另一个channel

演示一个transferTo()方法实例:
    FileChannel filefrom = raf.getChannel(); //raf、fbf为读到的文件流对象
    FileChannel fileto = rbf.getChannel();
    filefrom.transferTo(0, 20, fileto);

 

—— ——!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Java NIO模型中的FileChannel实现文件复制的代码实现: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class FileCopyDemo { public static void main(String[] args) throws Exception { FileInputStream inputStream = new FileInputStream("source.txt"); FileChannel inputChannel = inputStream.getChannel(); FileOutputStream outputStream = new FileOutputStream("target.txt"); FileChannel outputChannel = outputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (inputChannel.read(buffer) != -1) { buffer.flip(); outputChannel.write(buffer); buffer.clear(); } inputChannel.close(); outputChannel.close(); inputStream.close(); outputStream.close(); System.out.println("File copied successfully!"); } } ``` 以上代码中,我们首先定义了一个FileInputStream对象,并通过调用getChannel()方法创建一个FileChannel对象来进行文件读取。接着我们定义了一个FileOutputStream对象,并通过调用getChannel()方法创建一个FileChannel对象来进行文件写入。 然后我们使用allocate()方法创建一个1024字节大小的ByteBuffer对象。在while循环中,我们不断从inputChannel中读取数据到buffer中,然后将buffer的position设置为0,limit设置为buffer的当前position,接着通过write()方法将buffer中的数据写入到outputChannel中。 最后,我们需要关闭输入输出流和FileChannel对象,并输出文件复制成功的提示信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值