ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

FileChannel详细用法

2021-09-11 21:33:36  阅读:367  来源: 互联网

标签:long 用法 FileChannel RandomAccessFile 详细 import position channel


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);
}

标签:long,用法,FileChannel,RandomAccessFile,详细,import,position,channel
来源: https://blog.csdn.net/m0_51001708/article/details/120243503

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有