What kind of FileChannel object does the FileChannel.open(path) method return?

Is it still random access allowed as if it was as following?

RandomAccessFile ra = new RandomAccessFile("RandomIndeed","rw");
FileChannel fc1 = ra.getChannel();

  

What's the difference between fc1 and the following instance fc:

FileChannel fc = FileChannel.open(path);

  

Basically I would like to know what will be the differences between the 2 objects above-created, hence fc1 and fc

Thanks in advance.

 

The FileChannel instance got from RandomAccessFile instance carries the random access behaviour of the object it's been created, in this case fc1 is synced with ra object. You can see it's described in javadoc

Changing the channel's position, whether explicitly or by reading or writing bytes, will change the file position of the originating object, and vice versa. Changing the file's length via the file channel will change the length seen via the originating object, and vice versa. Changing the file's content by writing bytes will change the content seen by the originating object, and vice versa.

However the FileChannel instance which is created using FileChannel.open() which is fc doesn't have this behaviour. This is true for the FileChannel instances you got from Streams. It only guarantees that the view of the file is consistent among the objects created by the same program. Hope this might help you.

RandomAccessFile vs FileChannel.open(path);的更多相关文章

  1. RandomAccessFile、FileChannel、MappedByteBuffer读写文件

    s package com.nio; import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IO ...

  2. FileChannel详解

    经过前两篇文章的学习,相信对Channel有了一定的整体性认识.接下来通过学习本篇文章,更进一步认识Channel,学习FileChannel的细节 用途 特点 api 原理 一.用途 传统IO中的F ...

  3. Java NIO 文件通道 FileChannel 用法

    FileChannel 提供了一种通过通道来访问文件的方式,它可以通过带参数 position(int) 方法定位到文件的任意位置开始进行操作,还能够将文件映射到直接内存,提高大文件的访问效率.本文将 ...

  4. java的nio之:java的nio系列教程之FileChannel

    一:Java NIO的FileChannel===>Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. ===>FileChannel无法设置为非 ...

  5. Java基础知识强化之IO流笔记78:NIO之 FileChannel

    Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 1. 打开FileChannel 在 ...

  6. 【原创】java NIO FileChannel 学习笔记 新建一个FileChannel

    首先使用FileChannel 的open方法获取一个FileChannel对象.下面这段代码是FileChannel中open方法的代码. public static FileChannel ope ...

  7. Java NIO系列教程(二) Channel通道介绍及FileChannel详解

    目录: <Java NIO系列教程(二) Channel> <Java NIO系列教程(三) Channel之Socket通道> Channel是一个通道,可以通过它读取和写入 ...

  8. Java NIO系列教程(七) FileChannel

    Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 打开FileChannel 在使用F ...

  9. FileChannel指南

    推荐关注公众号:锅外的大佬 每日推送国外技术好文,帮助每位开发者更优秀地成长 原文链接:https://www.baeldung.com/java-filechannel 作者:baeldung 译者 ...

随机推荐

  1. centos6.8下hadoop3.1.1完全分布式安装指南

    前述:这篇文档是建立在三台虚拟机相互ping通,防火墙关闭,hosts文件修改,SSH 免密码登录,主机名修改等的基础上开始的. 一.传入文件 1.创建安装目录 mkdir /usr/local/so ...

  2. C# Winform ProgressBar+Labe 联动显示进度

    private void btnCount_Click(object sender, EventArgs e) { label1.Visible=true; progressBar.Visible = ...

  3. Linux中断管理 (3)workqueue工作队列【转】

    转自:https://www.cnblogs.com/arnoldlu/p/8659988.html 目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机 ...

  4. 3-1 Pandas-概述

    Pandas章节应用的数据可以在以下链接下载: https://files.cnblogs.com/files/AI-robort/Titanic_Data-master.zip Pandas:数据分 ...

  5. 目标检测论文解读1——Rich feature hierarchies for accurate object detection and semantic segmentation

    背景 在2012 Imagenet LSVRC比赛中,Alexnet以15.3%的top-5 错误率轻松拔得头筹(第二名top-5错误率为26.2%).由此,ConvNet的潜力受到广泛认可,一炮而红 ...

  6. 201871010133-赵永军《面向对象程序设计(java)》第十周学习总结

    201871010133-赵永军<面向对象程序设计(java)>第十周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这 ...

  7. 交互器中python中的帮助使用

    C:\Users\ceshi>pythonPython 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 b ...

  8. mysql版本引起的驱动问题

    mysql版本引起的驱动问题 com.mysql.jdbc.Driver 是 mysql-connector-java 5中的, com.mysql.cj.jdbc.Driver 是 mysql-co ...

  9. leetcode 561. 数组拆分 I

    为了理解这种方法,让我们从不同的角度来看待问题.我们需要形成数组元​​素的配对,使得这种配对中最小的总和最大.因此,我们可以查看选择配对中最小值的操作,比如 (a,b)(a,b) 可能会产生的最大损失 ...

  10. nwjs-简单应用于基本结构

    简单应用 1.在解压目录下创建文件夹dmeo01 2.在文件夹中创建index.html和package.json两个文件 3.index.html内容: <!DOCTYPE html> ...