RandomAccessFile vs FileChannel.open(path);
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);的更多相关文章
- RandomAccessFile、FileChannel、MappedByteBuffer读写文件
s package com.nio; import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IO ...
- FileChannel详解
经过前两篇文章的学习,相信对Channel有了一定的整体性认识.接下来通过学习本篇文章,更进一步认识Channel,学习FileChannel的细节 用途 特点 api 原理 一.用途 传统IO中的F ...
- Java NIO 文件通道 FileChannel 用法
FileChannel 提供了一种通过通道来访问文件的方式,它可以通过带参数 position(int) 方法定位到文件的任意位置开始进行操作,还能够将文件映射到直接内存,提高大文件的访问效率.本文将 ...
- java的nio之:java的nio系列教程之FileChannel
一:Java NIO的FileChannel===>Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. ===>FileChannel无法设置为非 ...
- Java基础知识强化之IO流笔记78:NIO之 FileChannel
Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 1. 打开FileChannel 在 ...
- 【原创】java NIO FileChannel 学习笔记 新建一个FileChannel
首先使用FileChannel 的open方法获取一个FileChannel对象.下面这段代码是FileChannel中open方法的代码. public static FileChannel ope ...
- Java NIO系列教程(二) Channel通道介绍及FileChannel详解
目录: <Java NIO系列教程(二) Channel> <Java NIO系列教程(三) Channel之Socket通道> Channel是一个通道,可以通过它读取和写入 ...
- Java NIO系列教程(七) FileChannel
Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 打开FileChannel 在使用F ...
- FileChannel指南
推荐关注公众号:锅外的大佬 每日推送国外技术好文,帮助每位开发者更优秀地成长 原文链接:https://www.baeldung.com/java-filechannel 作者:baeldung 译者 ...
随机推荐
- APP弱网测试方法
常用工具 •利用抓包工具 -Fiddler/Charles•使用chrome浏览器的开发者工具•使用手机自带的限速功能(只适用IOS设备)•需要硬件设备(路由器或者网卡) -NEWT/ATC/ ...
- 10.Redis 主从架构
作者:中华石杉 Redis 主从架构 单机的 redis,能够承载的 QPS 大概就在上万到几万不等.对于缓存来说,一般都是用来支撑读高并发的.因此架构做成主从(master-slave)架构,一主多 ...
- [PHP] 使用ftell和fseek函数直接定位文件位置获取部分数据
对于大文件只获取部分数据很有用 1.使用ftell函数可以获取当前指针的字节位置2.使用fseek函数可以直接定位到指定的位置3.读取指定字节的数据就可以部分获取文件内容了 <?php clas ...
- STM32F429驱动SDRAM
1 SDRAM控制原理 1.1 SDRAM信号线 1.2 SDRAM地址线 SDRAM包含有“A”以及“BA”两类地址线: A:行(Row)与列(Column)共用的地址线 BA:独立的用于指定SDR ...
- URL 路由系统 + views 函数
一.URL URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个URL调用这段 ...
- infinity新标签页失效
安装infinity新标签页后,无法生效,或者infinity新标签页突然失效了. 驱动精灵的问题 驱动精灵安装后,还会安装其它的软件,在卸载后,安装的软件还存在. 解决办法 光卸载驱动精灵还不够,还 ...
- 第24课经典问题(中)-----关于const对象的疑问
关于const对象的疑问const关键字能否修饰类的对象?如果可以,有什么特性?const关键字能够修饰对象const修饰的对象为只读对象只读对象的成员变量不允许被改变.(对象是只读的,成员变量不允许 ...
- 关于matlab tfdata的用法
加上'v',可以让输出的值由元胞数组改为数组直接输出:举个例子:h = tf([1 1],[1 2 5]);[num,den] = tfdata(h)可以看出输出的num和den为元胞数组的形式无 ...
- 了解html表单
html表单 表单的根标签:form form标签属性 action:处理表单业务的后台代码的位置(URL) method:提交方式 post get 默认值 enctype:encode type ...
- [RN] React Native ScrollView去掉自带的间隔
React Native ScrollView去掉自带的间隔 使用ScrollView时,自带了一个类似marginTop的效果,将其去掉 <ScrollView automaticallyAd ...