前言
眾所周知java提供file類,讓我們對(duì)文件進(jìn)行操作,下面就來(lái)簡(jiǎn)單整理了一下file類的用法。 話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧
1.基本概念
file:文件和目錄路徑名的抽象表示形式,代表文件或者文件夾。
2.構(gòu)造方法
1
2
3
4
5
6
7
8
|
// 根據(jù)parent抽象路徑名和child路徑名字符串創(chuàng)建一個(gè)新file實(shí)例 file(file parent, string child) // 通過(guò)將給定路徑名字符串轉(zhuǎn)換為抽象路徑名來(lái)創(chuàng)建一個(gè)新file實(shí)例 file(string pathname) // 根據(jù)parent路徑名字符串和child路徑名字符串創(chuàng)建一個(gè)新file實(shí)例 file(string parent, string child) // 通過(guò)將給定的file:uri轉(zhuǎn)換為一個(gè)抽象路徑名來(lái)創(chuàng)建一個(gè)新的file實(shí)例 file(uri uri) |
3.常用方法
(1).創(chuàng)建功能
// 創(chuàng)建此抽象路徑名指定的目錄
boolean mkdir()
// 創(chuàng)建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄
boolean mkdirs()
// 當(dāng)且僅當(dāng)不存在具有此抽象路徑名指定名稱的文件時(shí),不可分地創(chuàng)建一個(gè)新的空文件
boolean createnewfile()
// 在默認(rèn)臨時(shí)文件目錄中創(chuàng)建一個(gè)空文件,使用給定前綴和后綴生成其名稱
static file createtempfile(string prefix, string suffix)
// 在指定目錄中創(chuàng)建一個(gè)新的空文件,使用給定的前綴和后綴字符串生成其名稱
static file createtempfile(string prefix, string suffix, file directory)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// 構(gòu)造方法一 file file1 = new file( "f://file1" ); // 構(gòu)造方法二 file file2 = new file( "f://file1" , "file2" ); // 構(gòu)造方法三 file file3 = new file(file2, "file3.txt" ); // 創(chuàng)建目錄并返回是否創(chuàng)建成功,如果目錄存在則返回false boolean b1 = file1.mkdir(); system.out.println(b1); // true // 創(chuàng)建目錄 boolean b2 = file2.mkdir(); system.out.println(b2); // true // 創(chuàng)建文件 // 在f盤下創(chuàng)建/file1/file2/file3.txt文件 boolean b3 = file3.createnewfile(); system.out.println(b3); // true // 創(chuàng)建空文件并指定前綴和后綴 // 在f盤下創(chuàng)建/file1/file2/file4.....exe文件 file.createtempfile( "file4" , ".exe" , file2); |
注:三種構(gòu)造方法效果是等同的,沒(méi)有本質(zhì)區(qū)別;創(chuàng)建目錄mkdir()、mkdirs()方法,mkdirs()方法創(chuàng)建目錄時(shí),如果待創(chuàng)建目錄的上幾級(jí)目錄不存在則一并創(chuàng)建,mkdir()則只能創(chuàng)建單級(jí)目錄。
(2).刪除功能
// 刪除此抽象路徑名表示的文件或目錄
boolean delete()
1
2
3
4
|
//刪除目錄 system.out.println(file1.delete()); // false //刪除文件 system.out.println(file3.delete()); // true |
注:刪除操作時(shí),刪除的是目錄,則必須保證是空目錄。
(3).判斷功能
// 測(cè)試此抽象路徑名表示的文件或目錄是否存在
boolean exists()
// 測(cè)試此抽象路徑名表示的文件是否是一個(gè)目錄
boolean isdirectory()
// 測(cè)試此抽象路徑名表示的文件是否是一個(gè)標(biāo)準(zhǔn)文件
boolean isfile()
// 測(cè)試此抽象路徑名指定的文件是否是一個(gè)隱藏文件
boolean ishidden()
// 測(cè)試應(yīng)用程序是否可以讀取此抽象路徑名表示的文件
boolean canread()
// 測(cè)試應(yīng)用程序是否可以修改此抽象路徑名表示的文件
boolean canwrite()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
file file = new file( "f://hello" ); file file2 = new file(file, "blog.txt" ); // 判斷是否存在 if (!file.exists()) { // 創(chuàng)建目錄 file.mkdir(); } if (file2.exists()) { // 創(chuàng)建文件 file2.createnewfile(); } // 判斷是否是目錄 system.out.println(file.isdirectory()); // true // 判斷是否是文件 system.out.println(file.isfile()); // false system.out.println(file2.isdirectory()); // false system.out.println(file2.isfile()); // true system.out.println(file2.ishidden()); // false // 判斷是否是隱藏的 system.out.println(file2.ishidden()); // false // 判斷是否可讀 system.out.println(file2.canread()); // true // 判斷是否可寫 system.out.println(file2.canwrite()); // true |
注:可以自主修改文件可讀性,查看不同輸出。
(4).獲取功能
(1).基本獲取功能
// 返回由此抽象路徑名表示的文件或目錄的名稱
string getname()
// 返回此抽象路徑名的絕對(duì)路徑名形式
file getabsolutefile()
// 返回此抽象路徑名的絕對(duì)路徑名字符串
string getabsolutepath()
// 將此抽象路徑名轉(zhuǎn)換為一個(gè)路徑名字符串
string getpath()
// 返回此抽象路徑名表示的文件最后一次被修改的時(shí)間
long lastmodified()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
file file = new file( "f://hello" ); file file2 = new file(file, "blog.txt" ); // 判斷文件或目錄是否存在 if (!file.exists()) { // 不存在則創(chuàng)建 file.mkdir(); } if (file2.exists()) { // 創(chuàng)建文件 file2.createnewfile(); } //獲取文件名或者目錄名 system.out.println(file2.getname()); // blog.txt //獲取文件或目錄的絕對(duì)路徑 system.out.println(file2.getabsolutepath()); // f:\hello\blog.txt //獲取文件或目錄的路徑名(絕對(duì)路徑或者相對(duì)路徑) system.out.println(file2.getpath()); // f:\hello\blog.txt //獲取文件或目錄修改的最后時(shí)間返回毫秒值 system.out.println(file2.lastmodified()); // 1463734158963 |
(2).迭代獲取功能,過(guò)濾器功能
// 返回一個(gè)字符串?dāng)?shù)組,這些字符串指定此抽象路徑名表示的目錄中的文件和目錄
string[] list()
// 返回一個(gè)字符串?dāng)?shù)組,這些字符串指定此抽象路徑名表示的目錄中滿足指定過(guò)濾器的文件和目錄
string[] list(filenamefilter filter)
// 返回一個(gè)抽象路徑名數(shù)組,這些路徑名表示此抽象路徑名表示的目錄中的文件
file[] listfiles()
// 返回抽象路徑名數(shù)組,這些路徑名表示此抽象路徑名表示的目錄中滿足指定過(guò)濾器的文件和目錄
file[] listfiles(filefilter filter)
// 返回抽象路徑名數(shù)組,這些路徑名表示此抽象路徑名表示的目錄中滿足指定過(guò)濾器的文件和目錄
file[] listfiles(filenamefilter filter)
圖示:先看下f盤里面的東東
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
file file = new file( "f://" ); // 獲取抽象路徑名下的文件和目錄 string[] s = file.list(); // 過(guò)濾文件或者目錄名 string[] ss = file.list( new filenamefilter() { public boolean accept(file dir, string name) { // 返回以.png結(jié)尾的文件或者目錄名 // 控制返回值判斷是否添加到數(shù)組中 return name.endswith( ".png" ); } }); // 增強(qiáng)for輸出 for (string string : s) { system.out.print(string + " " ); // $recycle.bin android4.0黑馬 android視頻......等等 } // 增強(qiáng)for for (string string : ss) { system.out.print(string + " " ); // ic_ptr_loading.png ic_ptr_pull.png ic_ptr_release.png } // 獲取抽象路徑名下的文件和目錄對(duì)象 file[] files = file.listfiles(); // 獲取抽象路徑名下的文件和目錄對(duì)象,添加文件過(guò)濾 file[] files2 = file.listfiles( new filefilter() { public boolean accept(file pathname) { // 判斷是否是隱藏目錄 return (pathname.isdirectory()&&pathname.ishidden()); } }); // 獲取抽象路徑名下的文件和目錄對(duì)象,添加文件名過(guò)濾 file[] files3 = file.listfiles( new filenamefilter() { public boolean accept(file dir, string name) { // 判斷是否是以png結(jié)尾的文件 return ( new file(dir, name).isfile())&&name.endswith( ".png" ); } }); for (file f : files) { system.out.print(f.getname()+ " " ); //$recycle.bin android4.0黑馬 android視頻......等等 system.out.println(); for (file f : files2) { system.out.print(f.getname()+ " " ); //$recycle.bin ghos } system.out.println(); for (file f : files3) { system.out.print(f.getname()); //ic_ptr_loading.pngic_ptr_pull.pngic_ptr_release.png } |
(5).重命名功能
// 重新命名此抽象路徑名表示的文件
boolean renameto(file dest)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// 重新命名此抽象路徑名表示的文件 // boolean renameto(file dest) file file = new file( "f://" ); file[] files = file.listfiles(); for ( int i = 0 ; i < files.length; i++) { file f = files[i]; //判斷是否是以.png結(jié)尾的文件 if (f.isfile() && f.getname().endswith( ".png" )) { // 更改文件名,renameto()接收f(shuō)ile對(duì)象 ,這里該對(duì)象并未指定盤符 boolean b = f.renameto( new file( "pic" + i + ".png" )); system.out.println(b); // true // true // true } } |
圖示:
注:更改文件或目錄名時(shí),renameto()方法參數(shù)對(duì)象如果不指定盤的話,默認(rèn)會(huì)將文件剪切到項(xiàng)目目錄下(由上面截圖可以看到);指定盤符的話會(huì)根據(jù)指定位置剪切到該位置。renameto()方法相當(dāng)于剪切加重命名。
注:更多方法查看api
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:https://blog.csdn.net/magic_jss/article/details/51472205