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. orcal 根据打分时间计算打分情况

    create or replace function F_GET_TEST(in_ny in date,in_project_id in number ) return number is sRetu ...

  2. PADS常用画板过程

    转载:PADS LAYOUT的一般流程 http://www.doc88.com/p-9129306856292.html https://wenku.baidu.com/view/cc4e0b338 ...

  3. IDEA打开最近打开的项目以及关闭项目

    关闭的是当前项目

  4. Python_元组、字典内建方法详解

    目录 目录 前言 软件环境 元组Tuple count 查询一个元素在Tuple中的数量 index 查询元素在Tuple中的索引号 元组的遍历 字典Dictionary 创建一个字典对象 简单的创建 ...

  5. 阶段3 2.Spring_06.Spring的新注解_7 spring整合junit问题分析

    测试类重复代码的问题 这是之前的方式 运行findAll的方法,没有问题 测试人员不需要关心上面的方法,.应该关心的各个方法是否能够正常的运行 对于一个测试工程师,只要写完变量就可以测试了. 可以使用 ...

  6. JPA访问数据库的几种方式

    JPA访问数据库的几种方式 本文为原创,转载请注明出处:https://www.cnblogs.com/supiaopiao/p/10901793.html 1. Repository 1.1. 通过 ...

  7. SpringCloud常用注解有哪些?

    @Mapper: 注解写在你的Mapper映射接口上面 @SpringBootApplication: 写在主程序上面 @Configuration: 写在配置类上面 @Bean: 写在配置类中的返回 ...

  8. [ASP.NET] 解决点击控件下载文件没有响应的问题

    下载文件的方法是使用http响应输出流来实现的,使用到了response.write() 导致下载文件时点击控件出错,没有响应,也获取不了文件 是因为在母版页使用了updatepanel,因此回传时发 ...

  9. [官网]关于EPEL

    EPEL/zh-cn https://fedoraproject.org/wiki/EPEL/zh-cn Contents [hide]  1企业版 Linux 附加软件包(EPEL) 1.1什么是企 ...

  10. a++和++a的区别

    a++是先执行表达式后再自增,执行表达式时使用的是a的原值.++a是先自增再执行表达示,执行表达式时使用的是自增后的a.例:int a=0printf("%d",a++); //输 ...