zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    java的异常抛出和String类常用方法
    监控工具zabbix
    监控工具nagios
    监控工具cacti
    LB集群
    高可用集群(HA)配置
    vsftp虚拟用户登录配置详解
    Ubuntu中设置静态IP和DNS(转载)
    虚拟机克隆linux系统后需要做的网络设置
    CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27
  • 原文地址:https://www.cnblogs.com/felixzh/p/12013151.html
Copyright © 2011-2022 走看看