Writer :BYSocket(泥沙磚瓦漿木匠) java
微 博:BYSocket git
豆 瓣:BYSocket github
FaceBook:BYSocket express
Twitter :BYSocket apache
記得Java源碼是集合開始看的,寫了一系列集合相關的文章,受到不錯的評價。感謝各位讀者。我依舊會讀到老寫到老,並生動形象的寫出來心得體會。此次依舊是圖解,我研究IO這塊。 設計模式
Java IO – File的要點,應該是 數組
一、跨平臺問題的解決 安全
二、文件的安全 數據結構
三、文件的檢索方法 app
代請看一個簡單的小demo:(ps:開源項目java-core-learning地址:https://github.com/JeffLi1993)
importjava.io.File; importjava.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 * 列出目錄並排序 */ publicclassDirListT { publicstaticvoidmain(String[] args) { // 獲取當前目錄 File path = newFile(".");// .表示當前目錄 // 文件路徑名數組 String list[] = path.list(); // 對String文件名進行排序 Arrays.sort(list,String.CASE_INSENSITIVE_ORDER); // 打印 for(String dirItem : list) System.out.println(dirItem); } }
如圖,很容易注意到了,其目錄下的名字排序按字母並打印了。
先回顧下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的比較:
File,File到底是怎麼構成的。順着源碼,知道了File有幾個重要的屬性:
一、static private FileSystem fs
FileSystem : 對本地文件系統的抽象
二、String path 文件路徑名
三、內聯枚舉類
PathStatus 地址是否合法 ENUM類 private static enum PathStatus { INVALID, CHECKED };
四、prefixLength 前綴長度
以下,給出File相關核心的UML圖:
其實操做的是 FileSystem : 對本地文件系統的抽象,真正操做的是 FileSytem的派生類。經過源碼Ctrl+T發現以下:Win下操做的是 Win32FileSystem 和 WinNTFileSystem類。看來真正經過jvm,native調用系統的File是他們。
那Linux呢?所以,下了個Linux版本的JDK,解壓,找到rt.jar。而後java/io目錄中,找到了UnixFileSystem類。真相大白了!
因此能夠小結File操做源碼這樣調用的:中間不一樣JDK,實際上是不一樣的類調用本機native方法。
File 其實和咱們在系統中看的的文件同樣。就像咱們右鍵,屬性。能夠看到不少File的信息。Java File也有。下面是一個文件的相關方法詳情:
importjava.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方法詳細使用 */ publicclassFileMethodsT { privatestaticvoidfileData(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(" 是一個文件"); elseif(f.isDirectory()) System.out.println(" 是一個目錄"); } publicstaticvoidmain(String[] args) { // 獲取src目錄 File file = newFile("src"); // file詳細操做 fileData(file); } }
文件如何過濾呢?
之後獨立講吧,過濾涉及Fiter類。
一、看源碼很簡單,看數據結構。看如何調用。或者是有些設計模式
二、學東西,學一點一點深一點。太深很差,一點就夠了
三、泥瓦匠學習的代碼都在github上(同步osc git),歡迎你們點star,提意見,一塊兒進步。地址:https://github.com/JeffLi1993
Writer :BYSocket(泥沙磚瓦漿木匠)
微 博:BYSocket
豆 瓣:BYSocket
FaceBook:BYSocket
Twitter :BYSocket