天天看點

[Java開發之路](6)File類的使用

1. 構造方法

構造方法

描述

file(string pathname)

通過将給定的路徑名字元串轉換為抽象路徑名來建立一個新的檔案執行個體。

file(string parent , string child)

通過給定的父路徑名字元串和子路徑名字元串來建立一個新的檔案執行個體。

file(file parent , string child)

通過給定的父抽象路徑對象和子路徑名字元串來建立一個新的檔案執行個體。

file(uri uri)

通過給定的uri來建立一個新的檔案執行個體

<code>package com.qunar.bean;</code>

<code> </code>

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

<code>import java.net.urisyntaxexception;</code>

<code>public class filedemo {</code>

<code></code>

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

<code>string pathname = "e:\\recommended system";</code>

<code>// file(string pathname)</code>

<code>file file1 =new file(pathname);</code>

<code>// file(string parent,string child)</code>

<code>file file2 =new file(pathname,"train_data.txt");</code>

<code>// file(file parent,string child)</code>

<code>file file3 = new file(file1, "train_data.txt");</code>

<code>// file(uri uri)</code>

<code>// file file4 =new file(new uri(""));</code>

<code>// separator 跨平台分隔符</code>

<code>file file4 =new file("e:"+file.separator+"recommended system");</code>

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

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

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

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

<code>}</code>

運作結果:

   e:\recommended system

   e:\recommended system\train_data.txt

2. 建立與删除方法

方法

boolean createnewfile()

如果檔案存在傳回false,否則傳回true并且建立檔案 

boolean mkdir()

建立目錄

boolean mkdirs()

建立多級目錄

boolean delete()

如果檔案存在傳回true并且删除檔案,否則傳回false

boolean deleteonexit()

檔案使用完成後删除

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

<code>string pathname = "d:\\recommended system.txt";</code>

<code>// 建立檔案執行個體</code>

<code>file file = new file(pathname);</code>

<code>try {</code>

<code>// 如果檔案存在傳回false,否則傳回true并且建立檔案</code>

<code>if(file.createnewfile()){</code>

<code>system.out.println("建立檔案["+pathname+"]");</code>

<code>}//if</code>

<code>else{</code>

<code>system.out.println("檔案["+pathname+"]已存在");</code>

<code>}//else</code>

<code>} catch (ioexception e) {</code>

<code>e.printstacktrace();</code>

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

<code>//如果檔案存在傳回true并且删除檔案,否則傳回false</code>

<code>if(file.delete()){</code>

<code>system.out.println("删除檔案["+pathname+"]");</code>

<code>system.out.println("檔案["+pathname+"]不存在");</code>

<code>string pathname = "d:\\recommended system";</code>

<code>string pathname2 = "d:\\recommended system2\\paper\\news";</code>

<code>file file2 = new file(pathname2);</code>

<code>//如果目錄不存在傳回true并且建立目錄,否則傳回false</code>

<code>if(file.mkdir()){</code>

<code>system.out.println("建立目錄["+pathname+"]");</code>

<code>system.out.println("目錄["+pathname+"]已存在");</code>

<code>//如果多級目錄不存在傳回true并且建立多級目錄,否則傳回false</code>

<code>if(file2.mkdirs()){</code>

<code>system.out.println("建立多級目錄["+pathname2+"]");</code>

<code>system.out.println("多級目錄["+pathname2+"]已存在");</code>

3. 判斷方法

boolean canexecute()

判斷檔案是否可執行

boolean canread()

判斷檔案是否可讀

boolean canwrite()

判斷檔案是否可寫

boolean exists()

判斷檔案是否存在

boolean isdirectory()

判斷是否是目錄

boolean isfile()

判斷是否是檔案

boolean ishidden()

判斷是否隐藏

boolean isabsolute()

判斷是否是絕對路徑 檔案不存在也能判斷

<code>string pathname = "d:\\recommended system\\train_data.txt";</code>

<code>// 判斷檔案是否可執行</code>

<code>if(file.canexecute()){</code>

<code>system.out.println("檔案["+pathname+"]可執行");</code>

<code>system.out.println("檔案["+pathname+"]不可執行");</code>

<code>// 判斷檔案是否可讀</code>

<code>if(file.canread()){</code>

<code>system.out.println("檔案["+pathname+"]可讀");</code>

<code>system.out.println("檔案["+pathname+"]不可讀");</code>

<code>// 判斷檔案是否可寫</code>

<code>if(file.canwrite()){</code>

<code>system.out.println("檔案["+pathname+"]可寫");</code>

<code>system.out.println("檔案["+pathname+"]不可寫");</code>

<code>// 判斷檔案是否存在</code>

<code>if(file.exists()){</code>

<code>system.out.println("檔案["+pathname+"]存在");</code>

<code>// 判斷檔案是否是目錄</code>

<code>if(file.isdirectory()){</code>

<code>system.out.println("檔案["+pathname+"]是目錄檔案");</code>

<code>system.out.println("檔案["+pathname+"]不是目錄檔案");</code>

<code>// 判斷是否是一個絕對路徑</code>

<code>if(file.isabsolute()){</code>

<code>system.out.println("["+pathname+"]是絕對路徑");</code>

<code>system.out.println("["+pathname+"]不是絕對路徑");</code>

4.擷取方法

string getname()

傳回檔案或者是目錄的名稱

string getpath()

傳回路徑

string getabsolutepath()

傳回絕對路徑

string getparent()

傳回父目錄,如果沒有父目錄則傳回null

long lastmodified()

傳回最後一次修改的時間

long length()

傳回檔案的長度

file[] liseroots()

傳回系統可用的系統盤

string[] list() 

傳回一個字元串數組,給定路徑下的檔案或目錄名稱字元串

string[] list(filenamefilter filter)

傳回滿足過濾器要求的一個字元串數組

file[]  listfiles()

傳回一個檔案對象數組,給定路徑下檔案或目錄

file[] listfiles(filenamefilter filter)

傳回滿足過濾器要求的一個檔案對象數組

<code>// 傳回檔案或者目錄的名稱</code>

<code>system.out.println("getname-&gt;"+file.getname());</code>

<code>// 傳回路徑</code>

<code>system.out.println("getpath-&gt;"+file.getpath());</code>

<code>// 傳回檔案長度</code>

<code>system.out.println("length-&gt;"+file.length());</code>

<code>// 傳回給定路徑下的檔案和目錄字元串數組</code>

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

<code>for (string name : files) {</code>

<code>system.out.println("名稱:"+name);</code>

<code>}//for</code>

<code>file[] files2 = file.listfiles();</code>

<code>for (file file2 : files2) {</code>

<code>if(file2.isfile()){</code>

<code>system.out.println("檔案名稱:"+file2.getname());</code>

<code>else if(file2.isdirectory()){</code>

<code>system.out.println("目錄名稱:"+file2.getname());</code>

<code>// 列出可用的系統盤</code>

<code>file[] files3 = file.listroots();</code>

<code>for (file file3 : files3) {</code>

<code>system.out.println("檔案名稱:"+file3.getpath());</code>

getname-&gt;recommended system

getpath-&gt;e:\recommended system

length-&gt;16384

名稱:540135b87c6d0.csv

名稱:5403c3df31780.doc

名稱:baidu-salon-38-1.mp3

名稱:baidu-salon-38-1.pdf

名稱:textanalysis.rar

名稱:train_data.txt

名稱:中文停用詞表(比較全面_有1208個停用詞).txt

名稱:主題提起

名稱:協同過濾

名稱:天貓推薦算法.mp3

名稱:天貓推薦算法.pdf

名稱:推薦系統入門.zip

名稱:推薦系統實戰經驗與效果提升之道.pdf

檔案名稱:540135b87c6d0.csv

檔案名稱:5403c3df31780.doc

檔案名稱:baidu-salon-38-1.mp3

檔案名稱:baidu-salon-38-1.pdf

檔案名稱:textanalysis.rar

檔案名稱:train_data.txt

檔案名稱:中文停用詞表(比較全面_有1208個停用詞).txt

目錄名稱:主題提起

目錄名稱:協同過濾

檔案名稱:天貓推薦算法.mp3

檔案名稱:天貓推薦算法.pdf

檔案名稱:推薦系統入門.zip

檔案名稱:推薦系統實戰經驗與效果提升之道.pdf

檔案名稱:c:\

檔案名稱:d:\

檔案名稱:e:\

檔案名稱:f:\

檔案名稱:g:\

檔案名稱:h:\  

檔案過濾例子:

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

<code>// 檔案過濾</code>

<code>file[] files = file.listfiles(new filenamefilter() {</code>

<code>@override</code>

<code>public boolean accept(file file, string filename) {</code>

<code>return filename.endswith(".mp3");</code>

<code>});</code>

<code>// 列印輸出</code>

<code>for (file file2 : files) {</code>

<code>system.out.println("名稱:"+file2.getname());</code>

    名稱:baidu-salon-38-1.mp3

    名稱:天貓推薦算法.mp3

    名稱:百度推薦實踐.mp3  

列出檔案夾下的全部問價或目錄:

<code>public static void listallfile(file file){</code>

<code>if(!file.exists()){</code>

<code>throw new illegalargumentexception("目錄["+file+"]不存在");</code>

<code>if(!file.isdirectory()){</code>

<code>throw new illegalargumentexception("["+file+"]不是目錄");</code>

<code>// 列出指定路徑下的全部檔案或目錄</code>

<code>file[] files = file.listfiles();</code>

<code>for (file filename : files) {</code>

<code>// 判斷是否是目錄 如果是遞歸</code>

<code>if(filename.isdirectory()){</code>

<code>listallfile(filename);</code>

<code>// 否則列印輸出</code>

<code>system.out.println(filename.getpath());</code>

<code>// 列出全部檔案或目錄</code>

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

e:\recommended system\540135b87c6d0.csv e:\recommended system\5403c3df31780.doc e:\recommended system\baidu-salon-38-1.mp3 e:\recommended system\baidu-salon-38-1.pdf e:\recommended system\textanalysis.rar e:\recommended system\train_data.txt e:\recommended system\中文停用詞表(比較全面_有1208個停用詞).txt e:\recommended system\主題提起\中文新聞關鍵事件的主題句識别.pdf e:\recommended system\主題提起\加入時間因素的個性化資訊過濾技術.pdf e:\recommended system\主題提起\動态新聞主題資訊推薦系統設計.pdf ... e:\recommended system\協同過濾\patternrecognitionandmachinelearning.pdf e:\recommended system\協同過濾\一種改進的item-based協同過濾推薦算法.pdf e:\recommended system\協同過濾\一種電影個性化推薦系統的研究與實作.pdf e:\recommended system\協同過濾\個性化搜尋引擎中使用者協作推薦算法的研究.pdf e:\recommended system\協同過濾\個性化新聞推薦引擎中新聞分組聚類技術的研究與實作.caj e:\recommended system\協同過濾\協同過濾技術在個性化推薦中的運用.pdf e:\recommended system\協同過濾\基于協同過濾的個性化新聞推薦系統的研究與實作.caj e:\recommended system\協同過濾\基于矩陣分解的協同過濾算法.pdf .... e:\recommended system\天貓推薦算法.mp3 e:\recommended system\天貓推薦算法.pdf e:\recommended system\推薦系統入門.zip .....