天天看點

圖解 Java IO : 一、File源碼一、代碼小引入二、深入了解源碼三、小demo再來一發四、總結

java io – file的要點,應該是

1、跨平台問題的解決 2、檔案的安全 3、檔案的檢索方法

<a href="http://www.bysocket.com/?p=420#">?</a>

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><b>參數:</b></dt> <dd></dd> <code>pathname</code> – 路徑名字元串 <dt><b>抛出:</b></dt>

<dl><dd>二者,file實作了comparator接口,以便對filename進行排序。</dd></dl>

          一個對 <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 字首長度

<dl><dd>如下,給出file相關核心的uml圖:</dd></dl>

圖解 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再來一發四、總結

檔案如何過濾呢?

以後獨立講吧,過濾涉及filter類。

1、看源碼很簡單,看資料結構。看如何調用。或者是有些設計模式 2、學東西,學一點一點深一點。太深不好,一點就夠了