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 译者 ...
随机推荐
- 基于Morphia实现MongoDB按小时、按天聚合操作
MongoDB按照天数或小时聚合 需求 最近接到需求,需要对用户账户下的设备状态,分别按照天以及小时进行聚合,以此为基础绘制设备状态趋势图. 实现思路是启动定时任务,对各用户的设备状态数据分别按照小时 ...
- CentOs 7.*中配置安装phpMyAdmin的完整步骤记录
phpMyAdmin是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的资料库管理工具.下面这篇文章主要给大家介绍了关于CentOs 7.*中配置安装phpMyAdmin的相关资 ...
- easy-copy服务器文件拷贝简易小工具
github:easy-copy import os import sys import time import paramiko as pm ''' host格式: { "ip" ...
- 10wx.showToast消息提示框 wx.showModal模态对话框
1==>wx.showToast 弹出层 在界面交互中 显示消息提示框 它是一个消失提示框 提示用户成功 或者失败等消息 <button size='mini' bindtap='hanl ...
- 201871010104-陈园园 《面向对象程序设计(java)》第八周学习总结
201871010104-陈园园 <面向对象程序设计(java)>第八周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- C++使用OpenCV打印运行时间
代码 int64 tall = cv::getTickCount(); std::cout<< "time:" << (cv::getTickCount() ...
- Tkinter--Text文本框样例
#-*- coding:utf-8 -*- """ Text 文本框样例 实现功能有:Ctrl+a全选文本, 竖向滚动条,横向滚动条(不自动换行) 自动缩放 有谁知道全选 ...
- 高效Redis工具类
一.引言 本篇博客以redis缓存为主.至于什么是redis缓存?还有没有其它的缓存?哪个缓存的性能会更好?这里就不一一做介绍了!(有兴趣的可以自己去百度一下) 在日常的开发中,我们或多或少(必须)的 ...
- Python进阶-XIII 导入模块和包 异常处理
一.模块的导入 1).import # 测试一:money与my_module.money不冲突 import my_module money=10 print(my_module.money) '' ...
- CF1149D Abandoning Roads(图论,最短路,状态压缩,最小生成树)
题目大意:$n$ 个点,$m$ 条边的无向图,边权只有两种,小的为 $a$,大的为 $b$. 对于每个点 $p$,询问在这张图所有的最小生成树上,$1$ 到 $p$ 的最短距离的最小值. $2\le ...