Java中File類的用法

2021-09-14 02:28:28 字數 4201 閱讀 7000

檔案和目錄路徑名的抽象表現形式

方法名方法解釋

canread()

測試應用程式是否可以讀取此抽象路徑名表示的檔案

canwrite()

測試應用程式是否可以修改此抽象路徑名表示的檔案

compareto(file pathname)

按字母順序比較兩個抽象路徑名

canexecute()

測試應用程式是否可以執行此抽象路徑名表示的檔案

createnewfile()

當且僅當不存在具有此抽象路徑名指定名稱的檔案時,不可分地建立乙個新的空檔案

createtempfile(string prefix, string suffix)

在預設臨時檔案目錄中建立乙個空檔案,使用給定字首和字尾生成其名稱

createtempfile(string prefix, string suffix, file directory)

在指定目錄中建立乙個新的空檔案,使用給定的字首和字尾字串生成其名稱

delete()

刪除此抽象路徑名表示的檔案或目錄

void deleteonexit()

在虛擬機器終止時,請求刪除此抽象路徑名表示的檔案或目錄

boolean equals(object obj)

測試此抽象路徑名與給定物件是否相等

boolean exists()

測試此抽象路徑名表示的檔案或目錄是否存在

file getabsolutefile()

返回此抽象路徑名的絕對路徑名形式

string getabsolutepath()

返回此抽象路徑名的絕對路徑名字串

file getcanonicalfile()

返回此抽象路徑名的規範形式

string getcanonicalpath()

返回此抽象路徑名的規範路徑名字串

long getfreespace()

返回此抽象路徑名指定的分割槽中未分配的位元組數

string getname()

返回由此抽象路徑名表示的檔案或目錄的名稱

string getparent()

返回此抽象路徑名父目錄的路徑名字串;如果此路徑名沒有指定父目錄,則返回 null

file getparentfile()

返回此抽象路徑名父目錄的抽象路徑名;如果此路徑名沒有指定父目錄,則返回 null

string getpath()

將此抽象路徑名轉換為乙個路徑名字串

long gettotalspace()

返回此抽象路徑名指定的分割槽大小

long getusablespace()

返回此抽象路徑名指定的分割槽上可用於此虛擬機器的位元組數

int hashcode()

計算此抽象路徑名的雜湊碼

boolean isabsolute()

測試此抽象路徑名是否為絕對路徑名

boolean isdirectory()

測試此抽象路徑名表示的檔案是否是乙個目錄

boolean isfile()

測試此抽象路徑名表示的檔案是否是乙個標準檔案

boolean ishidden()

測試此抽象路徑名指定的檔案是否是乙個隱藏檔案

long lastmodified()

返回此抽象路徑名表示的檔案最後一次被修改的時間

long length()

返回由此抽象路徑名表示的檔案的長度

string list()

返回乙個字串陣列,這些字串指定此抽象路徑名表示的目錄中的檔案和目錄

string list(filenamefilter filter)

返回乙個字串陣列,這些字串指定此抽象路徑名表示的目錄中滿足指定過濾器的檔案和目錄

file listfiles()

返回乙個抽象路徑名陣列,這些路徑名表示此抽象路徑名表示的目錄中的檔案

file listfiles(filefilter filter)

返回抽象路徑名陣列,這些路徑名表示此抽象路徑名表示的目錄中滿足指定過濾器的檔案和目錄

file listfiles(filenamefilter filter)

返回抽象路徑名陣列,這些路徑名表示此抽象路徑名表示的目錄中滿足指定過濾器的檔案和目錄

static file listroots()

列出可用的檔案系統根

boolean mkdir()

建立此抽象路徑名指定的目錄

boolean mkdirs()

建立此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄

boolean renameto(file dest)

重新命名此抽象路徑名表示的檔案

boolean setexecutable(boolean executable)

設定此抽象路徑名所有者執行許可權的乙個便捷方法

boolean setexecutable(boolean executable, boolean owneronly)

設定此抽象路徑名的所有者或所有使用者的執行許可權

boolean setlastmodified(long time)

設定此抽象路徑名指定的檔案或目錄的最後一次修改時間

boolean setreadable(boolean readable)

設定此抽象路徑名所有者讀許可權的乙個便捷方法

boolean setreadable(boolean readable, boolean owneronly)

設定此抽象路徑名的所有者或所有使用者的讀許可權

boolean setreadonly()

標記此抽象路徑名指定的檔案或目錄,從而只能對其進行讀操作

boolean setwritable(boolean writable)

設定此抽象路徑名所有者寫許可權的乙個便捷方法

boolean setwritable(boolean writable, boolean owneronly)

設定此抽象路徑名的所有者或所有使用者的寫許可權

string tostring()

返回此抽象路徑名的路徑名字串

uri touri()

構造乙個表示此抽象路徑名的 file: uri

注:摘自api手冊

/*file的獲取功能*/

public class filedemo

//獲得 getparent() 獲取父路徑

public static void fun4()

//獲得絕對路徑 絕對位置下的工程根目錄

public static void fun3()

//length() 獲得檔案的位元組數

public static void fun2()

//獲取功能 放回路徑中 表示的檔案或者檔名 獲取路徑中最後部分的名字

public static void fun()

}

/* file判斷功能*/

public class filedemo

//判斷封裝的路徑是不是資料夾

public static void fun2()

} // 判斷路徑是否存在 exists()

public static void fun1()

}

// 獲取功能   所有的根目錄

public class filedemo2

//返回的只有檔名

public static void fun2()

}// 遍歷乙個目錄 返回全路徑+檔名

public static void fun() }

}

// 建立檔案功能   如果檔案已經存在  不再建立

public class filedemo2

//刪除

public static void fun3()

//建立目錄

public static void fun2() throws ioexception

public static void fun() throws ioexception

}

public static void main(string args) 

}

Java 中File類的使用

1.file類的建構函式 file file new file string pathname 通過定義路徑名字字串轉換為抽象路徑來建立乙個file例項。file file new file string parent,string child 根據父路徑和子路徑字串建立乙個新的file物件。包括檔...

File類的用法

使用file類建立乙個檔案的放法 1.file file1 new file string lujingming 2.file file2 new file string parentpath,string childpath 3.file file3 new file file1,lujingmi...

Java的File類操作

file檔案操作類 一 建立檔案類 file file new file filename filename可以是資料夾或者檔案 二 在進行檔案的操作時 若操作的物件是檔案,那麼在進行檔案的建立 刪除 複製之前,需要做如下操作 1 需要判斷檔案是否存在 2 以及file所指向的物件是乙個檔案還是乙個...