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解决锁表

    1.查看历史运行纪录 select * from dba_jobs_running: 2查看锁住的sid和pid select s.sid,s.serial# fromv$session s wher ...

  2. 1.5 log4j使用教程

    日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...

  3. DPI的理解

    DPI(Dots Per Inch,每英寸点数)是一个量度单位,用于点阵数码影像,指每一英寸长度中,取样.可显示或输出点的数目. DPI是打印机.鼠标等设备分辨率的度量单位.是衡量打印机打印精度的主要 ...

  4. 代码实现:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n

    import java.util.Scanner; //编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n public ...

  5. redis的LRU策略理解

    首先看下serverCron中,服务器每次循环执行的时候,都会刷新server.lrulock. int serverCron(struct aeEventLoop *eventLoop, long ...

  6. 阶段3 2.Spring_10.Spring中事务控制_4 spring中事务控制的一组API

    分析aop的 xml 的代码.更直观一些 事务提交和回滚就是我们重复的代码 spring业余事务管理器,我们拿过来直接用就可以 提交和回滚的后面直接调用释放.所以释放资源之类就是多余的 在绑定连接到线 ...

  7. PyCharm安装+破解

    PyCharm 是一款功能强大的 Python 编辑器,具有跨平台性,鉴于目前最新版 PyCharm 使用教程较少,为了节约时间,来介绍一下 PyCharm 在 Windows下是如何安装的. 这是 ...

  8. java:maven(maven-ssm(聚合,分包开发))

    1.maven-ssm: maven-ssm_diy: pom.xml: <?xml version="1.0" encoding="UTF-8"?> ...

  9. SpringMVC以POST提交表单中文乱码解决方案。

    在web.xml中添加字符集过滤器: <filter> <filter-name>characterEncodingFilter</filter-name> < ...

  10. Windows客户端 Linux服务器通讯 字符编码问题

    Windows下的字符编码默认是gb2312 在Linux下需要转成utf8 才能正确的看到对应的中文编码 提供转换函数 /*------------------------------------- ...