Java 教程
Java文件類以抽象的方式代表文件名和目錄路徑名。該類主要用于文件和目錄的創(chuàng)建、文件的查找和文件的刪除等。
File對(duì)象代表磁盤中實(shí)際存在的文件和目錄。通過以下構(gòu)造方法創(chuàng)建一個(gè)File對(duì)象。
通過給定的父抽象路徑名和子路徑名字符串創(chuàng)建一個(gè)新的File實(shí)例。
File(File parent, String child);
通過將給定路徑名字符串轉(zhuǎn)換成抽象路徑名來創(chuàng)建一個(gè)新 File 實(shí)例。
File(String pathname)
根據(jù) parent 路徑名字符串和 child 路徑名字符串創(chuàng)建一個(gè)新 File 實(shí)例。
File(String parent, String child)
通過將給定的 file: URI 轉(zhuǎn)換成一個(gè)抽象路徑名來創(chuàng)建一個(gè)新的 File 實(shí)例。
File(URI uri)
創(chuàng)建File對(duì)象成功后,可以使用以下列表中的方法操作文件。
序號(hào) | 方法描述 |
---|---|
1 |
public String getName() 返回由此抽象路徑名表示的文件或目錄的名稱。 |
2 |
public String getParent()、 ?返回此抽象路徑名的父路徑名的路徑名字符串,如果此路徑名沒有指定父目錄,則返回 null 。 |
3 |
public File getParentFile() 返回此抽象路徑名的父路徑名的抽象路徑名,如果此路徑名沒有指定父目錄,則返回 null 。 |
4 |
public String getPath() 將此抽象路徑名轉(zhuǎn)換為一個(gè)路徑名字符串。 |
5 |
public boolean isAbsolute() 測試此抽象路徑名是否為絕對(duì)路徑名。 |
6 |
public String getAbsolutePath() 返回抽象路徑名的絕對(duì)路徑名字符串。 |
7 |
public boolean canRead() 測試應(yīng)用程序是否可以讀取此抽象路徑名表示的文件。 |
8 |
public boolean canWrite() 測試應(yīng)用程序是否可以修改此抽象路徑名表示的文件。 |
9 |
public boolean exists() 測試此抽象路徑名表示的文件或目錄是否存在。 |
10 |
public boolean isDirectory() 測試此抽象路徑名表示的文件是否是一個(gè)目錄。 |
11 |
public boolean isFile() 測試此抽象路徑名表示的文件是否是一個(gè)標(biāo)準(zhǔn)文件。 |
12 |
public long lastModified() 返回此抽象路徑名表示的文件最后一次被修改的時(shí)間。 |
13 |
public long length() 返回由此抽象路徑名表示的文件的長度。 |
14 |
public boolean createNewFile() throws IOException 當(dāng)且僅當(dāng)不存在具有此抽象路徑名指定的名稱的文件時(shí),原子地創(chuàng)建由此抽象路徑名指定的一個(gè)新的空文件。 |
15 |
public boolean delete() ?刪除此抽象路徑名表示的文件或目錄。 |
16 |
public void deleteOnExit() 在虛擬機(jī)終止時(shí),請(qǐng)求刪除此抽象路徑名表示的文件或目錄。 |
17 |
public String[] list() 返回由此抽象路徑名所表示的目錄中的文件和目錄的名稱所組成字符串?dāng)?shù)組。 |
18 |
public String[] list(FilenameFilter filter) 返回由包含在目錄中的文件和目錄的名稱所組成的字符串?dāng)?shù)組,這一目錄是通過滿足指定過濾器的抽象路徑名來表示的。 |
19 |
public File[] listFiles() ??返回一個(gè)抽象路徑名數(shù)組,這些路徑名表示此抽象路徑名所表示目錄中的文件。 |
20 |
public File[] listFiles(FileFilter filter) 返回表示此抽象路徑名所表示目錄中的文件和目錄的抽象路徑名數(shù)組,這些路徑名滿足特定過濾器。 |
21 |
public boolean mkdir() 創(chuàng)建此抽象路徑名指定的目錄。 |
22 |
public boolean mkdirs() 創(chuàng)建此抽象路徑名指定的目錄,包括創(chuàng)建必需但不存在的父目錄。 |
23 |
public boolean renameTo(File dest) ?重新命名此抽象路徑名表示的文件。 |
24 |
public boolean setLastModified(long time) 設(shè)置由此抽象路徑名所指定的文件或目錄的最后一次修改時(shí)間。 |
25 |
public boolean setReadOnly() 標(biāo)記此抽象路徑名指定的文件或目錄,以便只可對(duì)其進(jìn)行讀操作。 |
26 |
public static File createTempFile(String prefix, String suffix, File directory) throws IOException 在指定目錄中創(chuàng)建一個(gè)新的空文件,使用給定的前綴和后綴字符串生成其名稱。 |
27 |
public static File createTempFile(String prefix, String suffix) throws IOException 在默認(rèn)臨時(shí)文件目錄中創(chuàng)建一個(gè)空文件,使用給定前綴和后綴生成其名稱。 |
28 |
public int compareTo(File pathname) 按字母順序比較兩個(gè)抽象路徑名。 |
29 |
public int compareTo(Object o) 按字母順序比較抽象路徑名與給定對(duì)象。 |
30 |
public boolean equals(Object obj) 測試此抽象路徑名與給定對(duì)象是否相等。 |
31 |
public String toString() ?返回此抽象路徑名的路徑名字符串。 |
下面的實(shí)例演示了File對(duì)象的使用:
以上實(shí)例編譯運(yùn)行結(jié)果如下:
Directory of /java bin is a directory lib is a directory demo is a directory test.txt is a file README is a file index.html is a file include is a directory