关于FileChannel的获取方式之open方法详解
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方法详解的更多相关文章
- Java中通过Class类获取Class对象的方法详解
方式1:通过Object类的getObject()方法 Person p = new Person(); Class c = p.getClass(); 方式2: 通过 类名.class 获取到字节码 ...
- $.ajax()方法详解 ajax之async属性 【原创】详细案例解剖——浅谈Redis缓存的常用5种方式(String,Hash,List,set,SetSorted )
$.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...
- Javascript获取图片原始宽度和高度的方法详解
前言 网上关于利用Javascript获取图片原始宽度和高度的方法有很多,本文将再次给大家谈谈这个问题,或许会对一些人能有所帮助. 方法详解 页面中的img元素,想要获取它的原始尺寸,以宽度为例,可能 ...
- “全栈2019”Java多线程第三十章:尝试获取锁tryLock()方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- PHP获取文件大小的方法详解
对于初入门的PHP新手来说,PHP获取文件大小这个功能实现,或许有一定的难度.但是相信新手小白们在看过本篇文章介绍后,一定能轻松掌握PHP获取文件大小的重要知识! 下面我们通过具体的代码示例,为大家详 ...
- (转)Spring JdbcTemplate 方法详解
Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...
- C++调用JAVA方法详解
C++调用JAVA方法详解 博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...
- CURL使用方法详解
php采集神器CURL使用方法详解 作者:佚名 更新时间:2016-10-21 对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程 ...
- PHP cURL应用实现模拟登录与采集使用方法详解
对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情景,file_get_co ...
随机推荐
- retrofit2+rxjava+okhttp网络请求实现
第一步:添加依赖: compile 'io.reactivex:rxandroid:1.2.0' compile 'com.squareup.retrofit2:adapter-rxjava:2.1. ...
- Reactjs之Axios、fetch-jsonp获取后台数据
1.新增知识点 /** Axios获取服务器数据(无法跨域,只能让后台跨域获取数据) react获取服务器APi接口的数据: react中没有提供专门的请求数据的模块.但是我们可以使用任何第三方请求数 ...
- Thymeleaf 页面表达式基础
转自:http://www.cnblogs.com/vinphy/p/4674247.html#undefined (一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个 ...
- Java基础面试题集(一)
Java基础面试题 一.面向对象编程(OOP) 7 二.常见的Java问题 7 2.1.什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? 7 2.2.JDK和JRE的区别是什么? ...
- React Native调用系统浏览器
import { Linking} from 'react-native'; //使用系统浏览器访问指定URLexport const contactBaidu = () => { var ba ...
- char* a = "abc" 和 char a[] = "abc" 之间的区别
char* a = "abc"; 声明了一个字符类型的指针a,并为它赋值初始值为"abc",a的值是字符串"abc"的首地址[第一个字符的地 ...
- 企业应用学习-git学习
1.git的基本使用 git与svn的区别 GIT 是分布式的,SVN 不是:这是 GIT 和其它非分布式的版本控制系统,例如 SVN,CVS 等,最核心的区别. GIT 把内容按元数据方式存储,而 ...
- Python 命令行解析模块 —— argparse
argparse是python标准库里面用来处理命令行参数的库,基本使用步骤如下: 1.import argparse 导入模块 2.parser = argparse.ArgumentPars ...
- 【神经网络与深度学习】caffe+VS2013+Windows无GPU快速配置教程
首先来一波地址: happynear大神的第三方caffe:http://blog.csdn.net/happynear/article/details/45372231 Neil Z大神的第三方ca ...
- setter 和 getter 高级 以及内存管理初级
setter 和 getter 的演变,紧接setter 和 getter 初级 1.@property 和 @synthesize 这两个关键字的出现,就是为了剔除代码中的setter方法和get ...