前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NIO系列(三)——FileChannel文件通道

NIO系列(三)——FileChannel文件通道

作者头像
逝兮诚
发布2019-10-30 13:38:41
6860
发布2019-10-30 13:38:41
举报
文章被收录于专栏:代码人生

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/luo4105/article/details/73650036

FileChannel

文件通道FileChannel,是堵塞的。作用是文件的连接和读取。

打开FileChannel

FileChannel不能直接打开,我们可以使用RandomAccessFile、InputStream、OutputStream来获取FileChannel实例。下面是RandomAccessFile获取实例

代码语言:javascript
复制
RandomAccessFile aFile = new RandomAccessFile("nio-data.txt","rw");
FileChannel inChannel = aFile.getChannel();

可以使用Buffer缓存来读写数据

读取数据

代码语言:javascript
复制
ByteBuffer bBuffer = ByteBuffer.allocate(48);
int readSize;
do {
    readSize = fileChannel.read(bBuffer);
    bBuffer.flip();
    System.out.println(Charset.forName("UTF-8").decode(bBuffer));
    bBuffer.clear();
} while (readSize != -1);

写数据

代码语言:javascript
复制
String newData = "\nNewString to write to file..." + System.currentTimeMillis();
ByteBuffer bBuffer = ByteBuffer.allocate(48);
fileChannel.position(fileChannel.size()); // 定位末尾,接着输入
bBuffer.put(newData.getBytes("UTF-8"));
bBuffer.clear();
while (bBuffer.hasRemaining()){
    fileChannel.write(bBuffer);
}
bBuffer.flip();

补充方法

FileChannel的size方法可以获得实例返回的文件大小。如:

代码语言:javascript
复制
long fileSize= channel.size();

FileChannel的truncate方法可以截取文件。截取文件时,文件将指定长度后面的部分将被删除。

代码语言:javascript
复制
channel.truncate(1024);

这个例子截取文件的前1024个字节。

FileChannel的force方法可以将通道中的缓存数据强制写到硬盘上,在操作系统中默认写入是调用write(),但是它会有缓存存在,可以调用sync和fsync函数,它会把缓存的数据强制写入。如:

代码语言:javascript
复制
channel.force(true);

代码

代码语言:javascript
复制
public classFileChannelTest {
    private RandomAccessFile accessFile;
    private FileChannel fileChannel;
 
    FileChannelTest(){
       try {
           accessFile = new RandomAccessFile("nio-data.txt","rw");
           fileChannel = accessFile.getChannel();
       }catch(FileNotFoundException e) {
           e.printStackTrace();
       }
    }
 
    /**
     * FileChannel读数据
     */
    public void readFileChannel() {
       try {
           ByteBufferbBuffer= ByteBuffer.allocate(48);
           int readSize;
           do {
              readSize = fileChannel.read(bBuffer);
              bBuffer.flip();
              System.out.println(Charset.forName("UTF-8").decode(bBuffer));
              bBuffer.clear();
           }while(readSize!= -1);
       }catch(IOException e) {
           e.printStackTrace();
       }finally{
           try {
              fileChannel.close();
           }catch(IOException e) {
              e.printStackTrace();
           }
       }
    }
 
    /**
     * FileChannel写数据
     */
    public void writeFileChannel() {
       try {
           StringnewData= "\nNew String to write tofile..." + System.currentTimeMillis();
           ByteBufferbBuffer= ByteBuffer.allocate(48);
           fileChannel.position(fileChannel.size()); // 定位末尾,接着输入
           bBuffer.put(newData.getBytes("UTF-8"));
           bBuffer.clear();
           while (bBuffer.hasRemaining()) {
              fileChannel.write(bBuffer);
           }
           bBuffer.flip();
       }catch(UnsupportedEncodingException e) {
           e.printStackTrace();
       }catch(IOException e) {
           e.printStackTrace();
       }finally{
           try {
              fileChannel.close();
           }catch(IOException e) {
              e.printStackTrace();
           }
        }
    }
 
    public static void main(String[] args) {
       FileChannelTestwriteCannel= newFileChannelTest();
       writeCannel.writeFileChannel();
       FileChannelTestreadCannel= newFileChannelTest();
       readCannel.readFileChannel();
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/06/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • FileChannel
    • 打开FileChannel
      • 读取数据
        • 写数据
          • 补充方法
            • 代码
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档