天天看点

图解 Java IO : 一、File源码一、代码小引入二、深入理解源码三、小demo再来一发四、总结

java io – file的要点,应该是

1、跨平台问题的解决 2、文件的安全 3、文件的检索方法

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

<code>import</code> <code>java.io.file;</code>

<code>import</code> <code>java.util.arrays;</code>

<code>/*</code>

<code> </code><code>* copyright [2015] [jeff lee]</code>

<code> </code><code>*</code>

<code> </code><code>* licensed under the apache license, version 2.0 (the "license");</code>

<code> </code><code>* you may not use this file except in compliance with the license.</code>

<code> </code><code>* you may obtain a copy of the license at</code>

<code> </code><code>* unless required by applicable law or agreed to in writing, software</code>

<code> </code><code>* distributed under the license is distributed on an "as is" basis,</code>

<code> </code><code>* without warranties or conditions of any kind, either express or implied.</code>

<code> </code><code>* see the license for the specific language governing permissions and</code>

<code> </code><code>* limitations under the license.</code>

<code> </code><code>*/</code>

<code>/**</code>

<code> </code><code>* @author jeff lee</code>

<code> </code><code>* @since 2015-7-13 07:58:56</code>

<code> </code><code>* 列出目录并排序</code>

<code>public</code> <code>class</code> <code>dirlistt {</code>

<code>    </code><code>public</code> <code>static</code> <code>void</code> <code>main(string[] args) {</code>

<code>        </code><code>// 获取当前目录</code>

<code>        </code><code>file path = </code><code>new</code> <code>file(</code><code>"."</code><code>);</code><code>// .表示当前目录</code>

<code>        </code><code>// 文件路径名数组</code>

<code>        </code><code>string list[] = path.list();</code>

<code>        </code> 

<code>        </code><code>// 对string文件名进行排序</code>

<code>        </code><code>arrays.sort(list,string.case_insensitive_order);</code>

<code>        </code><code>// 打印</code>

<code>        </code><code>for</code><code>(string diritem : list)</code>

<code>            </code><code>system.out.println(diritem);</code>

<code>    </code><code>}</code>

<code>}</code>

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

图解 Java IO : 一、File源码一、代码小引入二、深入理解源码三、小demo再来一发四、总结

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

先回顾下api知识吧,

通过将给定路径名字符串转换为抽象路径名来创建一个新 <code>file</code> 实例。如果给定字符串是空字符串,那么结果是空抽象路径名。 <dl></dl> <dt>参数:</dt> <dd></dd> <code>pathname</code> – 路径名字符串 <dt>抛出:</dt>

<dl><dd></dd></dl>

二者,file实现了comparator接口,以便对filename进行排序。

          一个对 <code>string</code> 对象进行排序的 comparator,作用与 <code>comparetoignorecase</code> 相同。

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

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

图解 Java IO : 一、File源码一、代码小引入二、深入理解源码三、小demo再来一发四、总结

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

图解 Java IO : 一、File源码一、代码小引入二、深入理解源码三、小demo再来一发四、总结
1、static private filesystem fs      filesystem : 对本地文件系统的抽象 2、string path 文件路径名 3、内联枚举类      pathstatus 地址是否合法 enum类 private static enum pathstatus { invalid, checked }; 4、prefixlength 前缀长度

如下,给出file相关核心的uml图:

图解 Java IO : 一、File源码一、代码小引入二、深入理解源码三、小demo再来一发四、总结

其实操作的是 filesystem : 对本地文件系统的抽象,真正操作的是 filesytem的派生类。通过源码ctrl+t发现如下:win下操作的是 win32filesystem 和 winntfilesystem类。看来真正通过jvm,native调用系统的file是他们。

图解 Java IO : 一、File源码一、代码小引入二、深入理解源码三、小demo再来一发四、总结

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

图解 Java IO : 一、File源码一、代码小引入二、深入理解源码三、小demo再来一发四、总结

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

图解 Java IO : 一、File源码一、代码小引入二、深入理解源码三、小demo再来一发四、总结

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

39

40

41

42

43

44

45

46

47

48

<code> </code><code>* @since 2015-7-13 10:06:28</code>

<code> </code><code>* file方法详细使用</code>

<code>public</code> <code>class</code> <code>filemethodst {</code>

<code>    </code> 

<code>    </code><code>private</code> <code>static</code> <code>void</code> <code>filedata(file f) {</code>

<code>        </code><code>system.out.println(</code>

<code>            </code><code>" 绝对路径:"</code> <code>+ f.getabsolutepath() +</code>

<code>            </code><code>"\n 可读:"</code> <code>+ f.canread() +</code>

<code>            </code><code>"\n 可写:"</code> <code>+ f.canwrite() +</code>

<code>            </code><code>"\n 文件名:"</code> <code>+ f.getname() +</code>

<code>            </code><code>"\n 上级目录:"</code> <code>+ f.getparent() +</code>

<code>            </code><code>"\n 相对地址:"</code> <code>+ f.getpath() +</code>

<code>            </code><code>"\n 长度:"</code> <code>+ f.length() +</code>

<code>            </code><code>"\n 最近修改时间:"</code> <code>+ f.lastmodified()</code>

<code>            </code><code>);</code>

<code>        </code><code>if</code><code>(f.isfile())</code>

<code>            </code><code>system.out.println(</code><code>" 是一个文件"</code><code>);</code>

<code>        </code><code>else</code> <code>if</code><code>(f.isdirectory())</code>

<code>            </code><code>system.out.println(</code><code>" 是一个目录"</code><code>);</code>

<code>        </code><code>// 获取src目录</code>

<code>        </code><code>file file = </code><code>new</code> <code>file(</code><code>"src"</code><code>);</code>

<code>        </code><code>// file详细操作</code>

<code>        </code><code>filedata(file);</code>

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

图解 Java IO : 一、File源码一、代码小引入二、深入理解源码三、小demo再来一发四、总结

文件如何过滤呢?

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

1、看源码很简单,看数据结构。看如何调用。或者是有些设计模式 2、学东西,学一点一点深一点。太深不好,一点就够了