天天看點

類File

* file類常用的構造方法:

* (1)file(string s);//由s确定file對象的檔案名

* (2)file(string directory,string s);//由directory确定file對象的檔案目錄,由s确定檔案名

* (3)file(file parent,string s);//根據一個父file對象和一個子檔案/目錄得到file對象

* 建立功能:

* public boolean createnewfile();//建立檔案

* public boolean mkdir();//建立檔案夾

* public boolean mkdirs();//建立檔案夾,如果父檔案夾不存在,将會建立出來

* 删除功能:

* public boolean delete();//建立檔案

*

* 注意:

* a:如果建立檔案或者檔案夾時沒有寫上盤符路徑,那麼預設在項目路徑

* b:java的删除不走資源回收筒

* c:要删除一個檔案夾,該檔案夾不能包含其他檔案或者檔案夾

* 重命名功能:

* public boolean renameto(file dest);

* 如果路徑相同,就是重命名

* 如果路徑不相同,就是重命名并剪切

* 判斷功能:

* public boolean isdirectory();//判斷是否是目錄

* public boolean isfile();//判斷是否是檔案

* public boolean exists();//判斷是否存在

* public boolean canread();//判斷是否可讀

* public boolean canwrite();//判斷是否可寫

* public boolean ishidden();//判斷是否隐藏

* 基本擷取功能:

* public string getabsolutepath();//擷取絕對路徑

* public string getpath();//擷取相對路徑

* public string getname();//擷取名稱

* public long length();//擷取檔案大小,位元組數

* public long lastmodified();//擷取修改時間,毫秒值

* 進階擷取功能:

* public string[] list();//擷取指定目錄下的所有檔案或者檔案夾的名稱數組

* public file[] listfiles();//擷取指定目錄下的所有檔案或者檔案夾的file數組

第1種方法:

/*

* 判斷e盤目錄下是否有字尾名為.jpg的檔案,如果有,就輸出此檔案名稱

* 分析:

* a:封裝e判斷目錄

* b:擷取該目錄下所有檔案或者檔案夾的file數組

* c:周遊該file數組,得到每一個file對象,然後判斷

* d:是否是檔案

* 是:繼續判斷是否以.jpg結尾

* 是:就輸出該檔案名稱

* 否:跳過

* 否:跳過

* */

第2種方法:

* 擷取符合條件的檔案,并輸出

* 要想實作這個效果,就必須使用一個接口:檔案名稱過濾器

* public string[] list(filenamefilter filter);

* public file[] listfiles(filenamefilter filter);

* 要求:把e:\\目錄下所有的.jpg結尾的檔案的絕對路徑輸出

* a:封裝目錄

* b:擷取該目錄下所有的檔案或者檔案夾的file數組

* c:周遊該file數組,得到每一個file對象

* d:判斷該file對象是否是檔案夾

* 是:回到b

* 否:繼續判斷是否是以結尾

* 是:就輸出該檔案的絕對路徑

* 否:跳過

* 要求:遞歸删除帶内容的目錄

* 否:删除

* 計算機如何識别什麼時候該把兩個位元組轉換為一個漢字呢?

* 在計算機中中文的存儲需要兩個位元組:

* 第一個位元組肯定是負數。

* 第二個位元組不一定。