創(chuàng)建和刪除文件/目錄常用的file類的方法
1.boolean exists():判斷文件或目錄是否存在
2.boolean createnewfile():創(chuàng)建新文件
3.boolean delete():刪除文件
4.boolean mkdirs():遞歸創(chuàng)建多級目錄
5.file getparentfile():獲取上級目錄
1.創(chuàng)建/刪除文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
try { //在d盤demo目錄新建文件:test.txt file file = new file( "d:\\demo\\test.txt" ); //如果文件存在,刪除,不存在,創(chuàng)建 if (!file.exists()) { //不存在,創(chuàng)建 file.createnewfile(); } else { //存在,刪除 file.delete(); } } catch (exception e) { e.printstacktrace(); } |
•創(chuàng)建目錄
1
2
3
4
5
6
7
8
9
10
11
|
try { // 在d盤demo目錄新建目錄"\a\b\c" file file = new file( "d:\\demo\\a\\b\\c" ); if (!file.getparentfile().exists()) { //上級目錄不存在,創(chuàng)建上級目錄 file.getparentfile().mkdirs(); } file.mkdirs(); } catch (exception e) { e.printstacktrace(); } |
文件的操作
1.string getname():獲取文件名
2.long length():獲取文件大小,返回字節(jié)單位
3.file getparentfile():獲取 上級目錄的file對象
4.string getparent():獲取上級目錄路徑
5.long lastmodified():最后修改時間
6.boolean isfile():是否是文件
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
|
try { //使用構(gòu)造方法確定要操作的文件 file file = new file( "d:" +file.separator+ "demo" +file.separator+ "n.o.k.i.a.pptx" ); //獲取文件名 string name = file.getname(); system.out.println( "文件名:" +name); //獲取文件后綴 system.out.println( "文件后綴:" +name.substring(name.lastindexof( "." )+ 1 )); //獲取文件大小 long length = file.length(); //tyte double size = ( double )length / 1024 ; //byte->kb decimalformat format = new decimalformat( "0.00" ); //保留兩位小數(shù) system.out.println( "文件大小:" +format.format(size)); //獲取上級目錄 system.out.println(file.getparentfile()); system.out.println(file.getparent()); //獲取最后修改時間 long lastmodified = file.lastmodified(); simpledateformat dateformat = new simpledateformat( "yyyy-mm-dd hh:mm:ss" ); date date = new date(lastmodified); system.out.println(dateformat.format(date)); //是否是文件 system.out.println(file.isfile()); } catch (exception e) { e.printstacktrace(); } |
目錄的操作
1.string[] list():獲取目錄下的所有文件的string表示
2.file[] listfiles():獲取目錄下的所有文件的file表示
3.boolean isdirectory():是否是目錄
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
try { file file = new file( "d:" +file.separator+ "demo" ); string[] list = file.list(); for (string s : list) { system.out.println(s); } system.out.println( "-------------" ); //是否是目錄 system.out.println(file.isdirectory()); //統(tǒng)計一個目錄下子目錄和文件的數(shù)量 file[] listfiles = file.listfiles(); int filecount = 0 ; //文件數(shù)量 int directorycount = 0 ; //目錄數(shù)量 for (file f : listfiles) { if (f.isfile()) { filecount++; } else { directorycount++; } } system.out.println( "文件:" +filecount+ "個,目錄:" +directorycount+ "個" ); } catch (exception e) { e.printstacktrace(); } |
遞歸輸出目錄
程序調(diào)用自身
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//遞歸輸出目錄信息:目錄子目錄,文件 public static void print(file file) { //判斷是否是目錄 if (file.isdirectory()) { //是目錄,獲取子目錄及文件 file[] listfiles = file.listfiles(); if (listfiles != null ) { for (file f : listfiles) { print(f); } } } system.out.println(file); } |
總結(jié)
以上所述是小編給大家介紹的java創(chuàng)建刪除文件和目錄的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/liruli/article/details/80191254