当前位置: 首页 > news >正文

图解 Java IO : 一、File源码

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

记得Java源码是集合开始看的,写了一系列集合相关的文章,受到不错的评价。感谢各位读者。我依旧会读到老写到老,并生动形象的写出来心得体会。这次依旧是图解,我研究IO这块。

Java IO – File的要点,应该是

1、跨平台问题的解决

 

2、文件的安全

 

3、文件的检索方法

一、代码小引入

代请看一个简单的小demo:(ps:开源项目java-core-learning地址https://github.com/JeffLi1993

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

import java.io.File;

import java.util.Arrays;

/*

 * Copyright [2015] [Jeff Lee]

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *   http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

 

/**

 * @author Jeff Lee

 * @since 2015-7-13 07:58:56

 * 列出目录并排序

 */

public class DirListT {

    public static void main(String[] args) {

        // 获取当前目录

        File path = new File(".");// .表示当前目录

        // 文件路径名数组

        String list[] = path.list();

         

        // 对String文件名进行排序

        Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);

         

        // 打印

        for(String dirItem : list)

            System.out.println(dirItem);

    }

}

在eclipse中,右键run一下,可以得到如下的结果:

image

如图,很容易注意到了,其目录下的名字排序按字母并打印了。

先回顾下API知识吧,

首先构造函数 public File(String pathname)

通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。如果给定字符串是空字符串,那么结果是空抽象路径名。

参数:

pathname – 路径名字符串

抛出:

NullPointerException – 如果 pathname 参数为 null

二者,File实现了Comparator接口,以便对FileName进行排序

static Comparator<String>
     CASE_INSENSITIVE_ORDER
          一个对 String 对象进行排序的 Comparator,作用与 compareToIgnoreCase 相同。

三者, path.list()为什么会返回String[] filenams的数组呢?怎么不是List呢?

自问自答:这时候,我们应该去看看ArrayList的实现,ArrayList其实是动态的数组实现。动态,动态的弊端就是效率低。此时,返回一个固定的数组,而不是一个灵活的类容器,因为其目录元素是固定的。下面是ArrayList和数组Array的比较

绘图1

 

二、深入理解源码

File,File究竟是怎么构成的。顺着源码,知道了File有几个重要属性

clipboard

1、static private FileSystem fs

 

     FileSystem : 对本地文件系统的抽象

 

2、String path 文件路径名

 

3、内联枚举类 

     PathStatus 地址是否合法 ENUM类 private static enum PathStatus { INVALID, CHECKED };

 

4、prefixLength 前缀长度

如下,给出File相关核心的UML图

file

 

其实操作的是 FileSystem : 对本地文件系统的抽象,真正操作的是 FileSytem派生类。通过源码Ctrl+T发现如下:Win下操作的是 Win32FileSystem 和 WinNTFileSystem类。看来真正通过jvm,native调用系统的File是他们。

clipboard[1]

Linux呢?因此,下了个Linux版本的JDK,解压,找到rt.jar。然后java/io目录中,找到了UnixFileSystem类。真相大白了!

clipboard[2]

 

所以可以小结File操作源码这样调用的:中间不同JDK,其实是不同的类调用本机native方法

绘图1

三、小demo再来一发

File 其实和我们在系统中看的的文件一样。就像我们右键,属性。可以看到很多File的信息。Java File也有。下面是一个文件的相关方法详情:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

import java.io.File;

/*

 * Copyright [2015] [Jeff Lee]

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *   http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

 

/**

 * @author Jeff Lee

 * @since 2015-7-13 10:06:28

 * File方法详细使用

 */

public class FileMethodsT {

     

    private static void fileData(File f) {

        System.out.println(

            " 绝对路径:" + f.getAbsolutePath() +

            "\n 可读:" + f.canRead() +

            "\n 可写:" + f.canWrite() +

            "\n 文件名:" + f.getName() +

            "\n 上级目录:" + f.getParent() +

            "\n 相对地址:" + f.getPath() +

            "\n 长度:" + f.length() +

            "\n 最近修改时间:" + f.lastModified()

            );

        if(f.isFile())

            System.out.println(" 是一个文件");

        else if(f.isDirectory())

            System.out.println(" 是一个目录");

    }

     

    public static void main(String[] args) {

        // 获取src目录

        File file = new File("src");

        // file详细操作

        fileData(file);

    }

}

在eclipse中,右键run一下,可以得到如下的结果:大家应该都明白了吧。

image

文件如何过滤呢?

以后独立讲吧,过滤涉及Filter类。

四、总结

1、看源码很简单,看数据结构。看如何调用。或者是有些设计模式

 

2、学东西,学一点一点深一点。太深不好,一点就够了

 

3、泥瓦匠学习的代码都在github上(同步osc git),欢迎大家点star,提意见,一起进步。地址:https://github.com/JeffLi1993

转载于:https://my.oschina.net/chenshuang/blog/1014781

相关文章:

  • Linux下软件分类与安装
  • flume自定义拦截器
  • [sqoop] sqoop 小试牛刀
  • linux select函数详解【转】
  • 前端优化的技巧
  • Android view的测量及绘制
  • inspect a service on the swarm
  • HDU 1176
  • Lintcode---线段树查询(区间最大值)
  • C++加载位图跟SOCKET通信的编写
  • 踩到Framework7 Photo Browser 的一个坑
  • ZOJ 1649 Rescue(有敌人迷宫BFS)
  • linux平台从源码安装git【转】
  • java中super的作用
  • css 选择符中的 ,+,~,=,^,$,*,|,:,空格 的意思
  • [case10]使用RSQL实现端到端的动态查询
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Angular4 模板式表单用法以及验证
  • Apache Zeppelin在Apache Trafodion上的可视化
  • egg(89)--egg之redis的发布和订阅
  • JavaScript设计模式系列一:工厂模式
  • miaov-React 最佳入门
  • mysql innodb 索引使用指南
  • Quartz初级教程
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 诡异!React stopPropagation失灵
  • 容器服务kubernetes弹性伸缩高级用法
  • 数据结构java版之冒泡排序及优化
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​插件化DPI在商用WIFI中的价值
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • ###项目技术发展史
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • (16)Reactor的测试——响应式Spring的道法术器
  • (5)STL算法之复制
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (ZT)出版业改革:该死的死,该生的生
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (附源码)ssm高校实验室 毕业设计 800008
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (三)Hyperledger Fabric 1.1安装部署-chaincode测试
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (译)2019年前端性能优化清单 — 下篇
  • .gitignore文件使用
  • .net Signalr 使用笔记
  • .Net Winform开发笔记(一)
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • .NET6 命令行启动及发布单个Exe文件