FileChannel详细用法

FileChannel详解

第一步

使用FileChannel之前我们必须打开它.我们需要通过InputStream,OutStream或者RandomAccessFile来获取一个FileChannel实例.下面是通过RandomAccessFile打开FileChannel的实例:

RandomAccessFile aFile = new RandomAccessFile("D:\\gwt.txt","rw");
FileChannel channel = aFile.getChannel();

第二步

从FileChannel中读取数据

ByteBuffer buffer = ByteBuffer.allocate(1024);
int byteRead = channel.read(buffer);

首先,分配一个Buffer.从一个FileChannel中获取的数据被读到Buffer中.然后,调用FileChannel.read()方法.该方法将数据从FileChannel读取到Buffer中.read()方法返回的int值表示了有多少字节被读到Buffer中,如果返回-1,表示到了文件末尾.

第三步

使用FileChannel.write()方法向FileChannel写数据,该方法参数是一个Buffer.

如:

package FileChannel演示;

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

//写操作
public class FileChannelDemo2 {
    public static void main(String[] args) throws IOException {
        //打开FileChannel
        RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\gwt.txt","rw");
        FileChannel channel = randomAccessFile.getChannel();

        //创建buffer对象
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        String newData = "Hello";

        //写入内容
        buffer.put(newData.getBytes());
        buffer.flip();

        //FileChannel最终实现
        while (buffer.hasRemaining()) {
            channel.write(buffer);
        }

        //关闭
        channel.close();
    }
}

FileChannel.write()在while中循环调用,因此无法保证write()方法一次能向FileChannel写入字节,因此反复调用,直到buff中已经没有尚未写入通道的字节.

第四步

关闭FileChannel

inChannel.close();

FileChannel Position

当读取或写入FileChannel时,需要在特定position执行。你可以通过调用position()方法来获得FileChannel的当前position

你还可以通过调用position(long pos)来设置FileChannelposition

long pos = channel.position();
channel.position(pos + 123);

如果你设置的position超过了文件的大小,并且尝试从Channel读取数据,则会返回-1代表文件结尾。

如果你设置的position超过了文件的大小,并且尝试往Channel写入数据,文件会自动扩张至能放下position以及写入的数据。这个可能导致"file hole",即磁盘上的物理文件在写入的数据中存在漏洞(即中间有一段完全没有任何数据)。

FileChannel Size

FileChannelsize()方法返回这个Channel连接的文件大小。如下:

long fileSize = channel.size();

FileChannel Truncate

通过调用FileChannel.truncate()方法,你可以truncate一个文件。当你truncate一个文件,你会把其截断为指定的长度。如下:

channel.truncate(1024);

这个例子将文件截断为1024字节。

FileChannel Force

FileChannel.force()方法会将Channel里面还未写入的数据全部刷新到磁盘。操作系统可能会将数据缓存在内存里以提升性能,因此我们无法保证你写入Channel的数据都被写到了磁盘,直到你调用force()方法。

force()方法有一个boolean类型的参数,代表是否将文件元数据(比如权限等)也刷新到磁盘。

以下是刷新数据以及元数据到磁盘的例子:

channel.force(true);

FileChannel Transfer

通道之间的传输,可以直接传

package FileChannel演示;

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

public class FileChannelDemo3 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile1 = new RandomAccessFile("D:\\gwt.txt","rw");
        RandomAccessFile randomAccessFile2 = new RandomAccessFile("D:\\qwe.txt","rw");
        FileChannel fromChannel = randomAccessFile1.getChannel();
        FileChannel toChannel = randomAccessFile2.getChannel();
        long position = 0;
        long count = fromChannel.size();
        toChannel.transferFrom(fromChannel,position,count);
        randomAccessFile1.close();
        randomAccessFile2.close();
        System.out.println("结束");
    }
}

官方代码:

public abstract class FileChannel
        extends AbstractChannel
        implements ByteChannel, GatheringByteChannel, ScatteringByteChannel
{
        // There are more other methods
        public abstract long transferTo (long position, long count, WritableByteChannel target);
        public abstract long transferFrom (ReadableByteChannel src, long position, long count);
}
  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值