FileChannel.open(Path path, OpenOption... options);

例子使用JDK1.8

FileChannel open方法源码:

public static FileChannel open(Path path, OpenOption... options)
throws IOException
{
Set<OpenOption> set = new HashSet<OpenOption>(options.length);
Collections.addAll(set, options);
return open(path, set, NO_ATTRIBUTES);
}

 

继续查看源码:

public static FileChannel open(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException
{
     //FileChannel的对象,由FileSystemProvider提供
FileSystemProvider provider = path.getFileSystem().provider();
return provider.newFileChannel(path, options, attrs);
}

FileChannel的对象,看似由FileSystemProvider提供,我们继续跟代码

 public FileChannel newFileChannel(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException
{
throw new UnsupportedOperationException();
}

方法到这一步我们发现,该方法其实是一个空方法,我们查看FileSystemProvider的类结构,看是否在其子类中会有对应实现,如下:

我们可以看FileSystemProvider的实现类有两个,其中ZipFileSystemProvider提供了newFileChannel的方法实现,但是由于该类不是JDK的核心类,该类位于jdk1.8.0_131\jre\lib\ext\zipfs.jar,所有没有提供源码,不过我们可以通过反编译工具进行跟进去:

//ZipFileSystemProvider方法newFileChannel
//ZipFileSystemProvider类提供的方法newFileChannel其实还不是类的实现,继续看toZipPath方法
public FileChannel newFileChannel(Path paramPath, Set<? extends OpenOption> paramSet, FileAttribute<?>... paramVarArgs)
throws IOException
{
return toZipPath(paramPath).newFileChannel(paramSet, paramVarArgs);
}

ZipFileSystemProvider类提供的方法newFileChannel其实还不是类的实现,继续看toZipPath方法:

//ZipFileSystemProvider方法toZipPath
//我们看到这里的方法返回的是一个ZipPath类,也就是说上面的代码 toZipPath(paramPath).newFileChannel(paramSet, paramVarArgs);
//可以理解为ZipPath.newFileChannel(),没办法继续看ZipPath类
static final ZipPath toZipPath(Path paramPath)
{
if (paramPath == null) {
throw new NullPointerException();
}
if (!(paramPath instanceof ZipPath)) {
throw new ProviderMismatchException();
}
return (ZipPath)paramPath;
}

我们看到这里的方法返回的是一个ZipPath类,也就是说上面的代码  toZipPath(paramPath).newFileChannel(paramSet, paramVarArgs);以理解为ZipPath.newFileChannel(),没办法继续看ZipPath

//截取部分类属性
public class ZipPath
  implements Path
{
//2:继续跟ZipFileSystem类
  private final ZipFileSystem zfs;
  private final byte[] path;
  private volatile int[] offsets;
  private int hashcode = 0; FileChannel newFileChannel(Set<? extends OpenOption> paramSet, FileAttribute<?>... paramVarArgs)
    throws IOException
  {
   //1:老套路,继续看zfs属性是个啥
    return this.zfs.newFileChannel(getResolvedPath(), paramSet, paramVarArgs);
  }
}
 
 

继续ZipFileSystem源码:

//ZipFileSystem类方法newFileChannel
//我们终于找到了FileChannel在哪给我们实现了,我们可以看到这里是new了一个FileChannel(){},一般情况下我们知道new一个对象的语法:
// ClassA a = new Class(); 其实这里是省略了大括号 ClassA a = new Class(){}; 完整的写法在这,但是有个一问题
FileChannel newFileChannel(byte[] paramArrayOfByte, Set<? extends OpenOption> paramSet, FileAttribute<?>... paramVarArgs)
throws IOException
{
checkOptions(paramSet);
final boolean bool1 = (paramSet.contains(StandardOpenOption.WRITE)) || (paramSet.contains(StandardOpenOption.APPEND));
beginRead();
try
{ new FileChannel()
{
... return localFileChannel.write(paramAnonymousArrayOfByteBuffer, paramAnonymousInt1, paramAnonymousInt2); ... public int read(ByteBuffer paramAnonymousByteBuffer)
throws IOException
{
return localFileChannel.read(paramAnonymousByteBuffer);
}
};
}
finally
{
endRead();
}
}

 

关于FileChannel的获取方式之open方法详解的更多相关文章

  1. Java中通过Class类获取Class对象的方法详解

    方式1:通过Object类的getObject()方法 Person p = new Person(); Class c = p.getClass(); 方式2: 通过 类名.class 获取到字节码 ...

  2. $.ajax()方法详解 ajax之async属性 【原创】详细案例解剖——浅谈Redis缓存的常用5种方式(String,Hash,List,set,SetSorted )

    $.ajax()方法详解   jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...

  3. Javascript获取图片原始宽度和高度的方法详解

    前言 网上关于利用Javascript获取图片原始宽度和高度的方法有很多,本文将再次给大家谈谈这个问题,或许会对一些人能有所帮助. 方法详解 页面中的img元素,想要获取它的原始尺寸,以宽度为例,可能 ...

  4. “全栈2019”Java多线程第三十章:尝试获取锁tryLock()方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  5. PHP获取文件大小的方法详解

    对于初入门的PHP新手来说,PHP获取文件大小这个功能实现,或许有一定的难度.但是相信新手小白们在看过本篇文章介绍后,一定能轻松掌握PHP获取文件大小的重要知识! 下面我们通过具体的代码示例,为大家详 ...

  6. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  7. C++调用JAVA方法详解

    C++调用JAVA方法详解          博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...

  8. CURL使用方法详解

    php采集神器CURL使用方法详解 作者:佚名  更新时间:2016-10-21   对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程 ...

  9. PHP cURL应用实现模拟登录与采集使用方法详解

    对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情景,file_get_co ...

随机推荐

  1. Servlet请求参数的方式

    今天整理了以下几种常用的Servlet请求参数的方式,下面简单地介绍 1)getParameter(String key)返回一个字符串,获得name和key 一样的表单控件的数据,如果有重复的nam ...

  2. XPath2Doc,一个半自动采集网页生成Word Docx文件的工具,带企查查和天眼查模板

    原始出处:https://www.cnblogs.com/Charltsing/p/XPath2Doc.html 很多人需要从网站采集一些数据填写Word模板,手工操作费时费力还容易出错,所以我给朋友 ...

  3. vue项目如何部署到Tomcat中

    vue项目如何部署到Tomcat中 1,假设你要访问的项目名称为'hms' 2,在Tomcat的webapps下创建hms文件夹, 3,配置config/index.js文件,build: {} 选项 ...

  4. 代码实现:判断101-200之间有多少个素数(质数),并输出所有素数。 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

    package com.loaderman.Coding; /* 判断101-200之间有多少个素数(质数),并输出所有素数. 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能 ...

  5. emqttd学习教程(二):emqttd配置说明

    一.配置文件说明emqttd消息服务器通过 etc/ 目录下配置文件进行设置,主要配置文件包括: 配置文件 说明 etc/emq.conf 消息服务器配置文件etc/acl.conf 默认ACL规则配 ...

  6. 浅谈Excel开发:二 Excel 菜单系统(转)

    编辑器加载中...http://www.cnblogs.com/yangecnu/p/Excel-Menu-System-Introduction.html 在开始Excel开发之前,需要把架子搭起来 ...

  7. 占位图片placehold.it生成

    (1)默认:http://www.placehold.it/350x200/cccccc/969696.jpg/&text=loading.. (2)格式:http://www.placeho ...

  8. zabbix报警后不会自动消除解决

    http://www.cnblogs.com/zhongkai-27/p/9984597.html

  9. LeetCode.1033-移动石头直到连续(Moving Stones Until Consecutive)

    这是小川的第386次更新,第414篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第247题(顺位题号是1033).在a,b和c位置的数字线上有三块石头.每次,你在一个终点 ...

  10. 灰度图像的自动阈值分割(Otsu 法)

    关于otsu分割方法,这个文章讲的是最好的,清晰易懂,一看就是作者认真思考过的. 因为在看这个算法的时候我就想,如果一个很大的图像上,大部分像素值都在0 - 50范围内,但是有很小一块像素值在240的 ...