Java File類的簡(jiǎn)單使用教程(創(chuàng)建、刪除、遍歷與判斷是否存在等)
前言
Java文件類以抽象的方式代表文件名和目錄路徑名。該類本身不能用來(lái)讀數(shù)據(jù)或?qū)憯?shù)據(jù),它主要用于磁盤上文件和目錄的創(chuàng)建、文件的查找和文件的刪除。做一些非讀寫方面的工作,比如看看文件是否存在、是否可讀寫及遍歷文件目錄等等。要想讀寫數(shù)據(jù),必須和其它io流的類配合使用,比如FileInputStream、FileOutputStream等。File對(duì)象代表磁盤中實(shí)際存在的文件和目錄,以下就通過(guò)一些簡(jiǎn)單的列子介紹File的基本使用。
這是整個(gè)File簡(jiǎn)單使用的代碼:
1 package com.tianjh; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 /** 7 * Created on 2020/12/10 8 * File類的基本使用 9 * 10 */ 11 public class FileDemo { 12 public static void main(String[] args) { 13 String dirname = "D:/Demo"; 14 // 實(shí)例化一個(gè)File對(duì)象 15 File f1 = new File(dirname); 16 17 // 1. 遍歷指定目錄之下的所有文件 18 // 判斷f1對(duì)象是否是一個(gè)目錄 19 if (f1.isDirectory()) { 20 System.out.println("Directory of " + dirname); 21 String[] s = f1.list(); 22 // 遍歷s數(shù)組,取出數(shù)組中的元素進(jìn)行判斷 23 for (int i = 0; i < s.length; i++) { 24 File f = new File(dirname + "/" + s[i]); 25 if (f.isDirectory()) { 26 System.out.println(s[i] + " is a directory"); 27 } else { 28 System.out.println(s[i] + " is a file"); 29 } 30 } 31 } else { 32 // 不是一個(gè)目錄 33 System.out.println(dirname + " is not a directory"); 34 } 35 // expected output: 36 // Directory of D:/Demo 37 // BufferedInputStream.java is a file 38 // BufferedOutputStream.java is a file 39 // childFile is a directory 40 41 /* 42 * 2. 測(cè)試指定文件是否可執(zhí)行 43 * 測(cè)試應(yīng)用程序是否可以執(zhí)行此抽象路徑名表示的文件 44 * true: 當(dāng)且僅當(dāng)存在抽象路徑名,并允許應(yīng)用程序執(zhí)行該文件時(shí) 45 */ 46 System.out.println(dirname + " allowed to execute? " + f1.canExecute()); 47 // expected output: D:/Demo allowed to execute? true 48 49 50 /* 51 * 3. 測(cè)試指定文件是否可讀取 52 * 測(cè)試應(yīng)用程序是否可以讀取由此抽象路徑名表示的文件 53 * true: 當(dāng)且僅當(dāng)此抽象路徑名指定的文件存在并可由應(yīng)用程序讀取時(shí); 54 * false: 與true相反 55 */ 56 System.out.println(dirname + " allowed to read? " + f1.canRead()); 57 // expected output: D:/Demo allowed to read? true 58 59 /* 60 * 4. 測(cè)試指定文件是否可寫 61 * 測(cè)試應(yīng)用程序是否可以修改由此抽象路徑名表示的文件 62 * true: 當(dāng)且僅當(dāng)文件系統(tǒng)實(shí)際上包含由該抽象路徑名表示的文件并且允許應(yīng)用程序?qū)懭朐撐募r(shí); 63 * false: 與true相反 64 */ 65 System.out.println(dirname + " allowed to write? " + f1.canWrite()); 66 // expected output: D:/Demo allowed to write? true 67 68 /* 69 * 5. 比較抽象路徑名和參數(shù)抽象路徑名是否相等 70 * 比較兩個(gè)抽象的路徑名字典是否相等 等于零則相等,小于零則抽象路徑名字典小于參數(shù)路徑字典,大于則相反 71 * 比較規(guī)則按照字典順序進(jìn)行排序 72 */ 73 String s1 = "C:/Boot"; 74 // “D:/Demo” 與 "C:/Boot" 比較 75 System.out.println(f1.compareTo(new File(s1))); 76 // expected output: 1 77 String s2 = "D:/Deoo"; 78 // “D:/Demo” 與 "D:/Deoo" 比較 79 System.out.println(f1.compareTo(new File(s2))); 80 // expected output: -2 81 82 83 /* 84 * 6. 創(chuàng)建一個(gè)新文件 85 * 當(dāng)且僅當(dāng)具有該名稱的文件尚不存在時(shí),原子地創(chuàng)建一個(gè)由該抽象路徑名命名的新的空文件 86 * true: 如果命名文件不存在并被成功創(chuàng)建; 87 * false: 如果命名文件已經(jīng)存在 88 */ 89 File f3 = new File("/Boot"); 90 try { 91 System.out.println("/Boot file is created? " + f3.createNewFile()); 92 // expected output: /Boot file is created? false 93 } catch (IOException e) { 94 e.printStackTrace(); 95 } 96 97 /* 98 * 7. 創(chuàng)建一個(gè)目錄 99 * 創(chuàng)建由此抽象路徑名命名的目錄 100 */ 101 String dirnames = "D:/tmp/boot"; 102 File f4 = new File(dirnames); 103 // 創(chuàng)建一個(gè)文件夾,成功則返回true,失敗則返回false。 104 // 失敗表明File對(duì)象指定的路徑已經(jīng)存在,或者由于整個(gè)路徑還不存在,該文件夾不能被創(chuàng)建。 105 System.out.println("create mkdir is " + f4.mkdir()); 106 // expected output: create mkdir is true 107 108 109 /* 110 * 8. 創(chuàng)建一個(gè)目錄,包括其不存在的父級(jí)目錄 111 * 創(chuàng)建一個(gè)文件夾和它的所有父文件夾 失敗表明File對(duì)象指定的路徑已經(jīng)存在 112 */ 113 System.out.println("create mkdirs is " + f4.mkdirs()); 114 // expected output: create mkdirs is false 115 116 117 /* 118 * 9. 刪除文件或者目錄 119 * 刪除由此抽象路徑名表示的文件或目錄 120 * true當(dāng)且僅當(dāng)文件或目錄被成功刪除時(shí); false否則 121 */ 122 System.out.println(dirnames + " deleted is " + f4.delete()); 123 // expected output: D:/tmp/boot deleted is true 124 125 126 /* 127 * 10. 取得抽象路徑的名稱 128 * 取到抽象路徑名表示的文件或目錄的名稱 129 */ 130 System.out.println("getName is " + f1.getName()); 131 // expected output: getName is Demo 132 133 134 /* 135 * 11. 取得抽象路徑的字符串 136 * 獲得由抽象路徑名轉(zhuǎn)換為路徑名字符串 137 */ 138 System.out.println("getPath is " + f1.getPath()); 139 // expected output: getPath is D:\Demo 140 141 /* 142 * 12. 取得抽象路徑的絕對(duì)路徑 143 * 獲得此抽象路徑名的絕對(duì)路徑名字符串 144 */ 145 System.out.println("Absolute Path is " + f1.getAbsolutePath()); 146 // expected output: Absolute Path is D:\Demo 147 148 149 /* 150 * 13. 判斷抽象路徑指定的文件或目錄是否存在 151 * 測(cè)試此抽象路徑名表示的文件或目錄是否存在 152 * true: 當(dāng)且僅當(dāng)存在由此抽象路徑名表示的文件或目錄時(shí); 153 * false: 與true相反 154 */ 155 System.out.println(f1.exists() ? "exist" : "not"); 156 // expected output: exist 157 } 158 159 } FileDemo.Java
下面分別介紹常用的幾種方法:
1、遍歷指定目錄之下的所有文件( 遍歷" D:/Demo "中的所有文件及目錄)
D磁盤中Demo目錄的結(jié)果如下所示:
示例代碼:
String dirname = "D:/Demo"; // 實(shí)例化一個(gè)File對(duì)象 File f1 = new File(dirname); // 1. 遍歷指定目錄之下的所有文件 // 判斷f1對(duì)象是否是一個(gè)目錄 if (f1.isDirectory()) { System.out.println("Directory of " + dirname); String[] s = f1.list(); // 遍歷s數(shù)組,取出數(shù)組中的元素進(jìn)行判斷 for (int i = 0; i < s.length; i++) { File f = new File(dirname + "/" + s[i]); if (f.isDirectory()) { System.out.println(s[i] + " is a directory"); } else { System.out.println(s[i] + " is a file"); } } } else { // 不是一個(gè)目錄 System.out.println(dirname + " is not a directory"); } // expected output: // Directory of D:/Demo // BufferedInputStream.java is a file // BufferedOutputStream.java is a file // childFile is a directory
輸出結(jié)果:
2、測(cè)試指定文件是否可執(zhí)行
/* * 2. 測(cè)試指定文件是否可執(zhí)行 * 測(cè)試應(yīng)用程序是否可以執(zhí)行此抽象路徑名表示的文件 * true: 當(dāng)且僅當(dāng)存在抽象路徑名,并允許應(yīng)用程序執(zhí)行該文件時(shí) */ System.out.println(dirname + " allowed to execute? " + f1.canExecute()); // expected output: D:/Demo allowed to execute? true
3、測(cè)試指定文件是否可讀取
/* * 3. 測(cè)試指定文件是否可讀取 * 測(cè)試應(yīng)用程序是否可以讀取由此抽象路徑名表示的文件 * true: 當(dāng)且僅當(dāng)此抽象路徑名指定的文件存在并可由應(yīng)用程序讀取時(shí); * false: 與true相反 */ System.out.println(dirname + " allowed to read? " + f1.canRead()); // expected output: D:/Demo allowed to read? true
4、測(cè)試指定文件是否可寫
/* * 4. 測(cè)試指定文件是否可寫 * 測(cè)試應(yīng)用程序是否可以修改由此抽象路徑名表示的文件 * true: 當(dāng)且僅當(dāng)文件系統(tǒng)實(shí)際上包含由該抽象路徑名表示的文件并且允許應(yīng)用程序?qū)懭朐撐募r(shí); * false: 與true相反 */ System.out.println(dirname + " allowed to write? " + f1.canWrite()); // expected output: D:/Demo allowed to write? true
樣例2、3、4的結(jié)果可參考Demo 的屬性
5、比較抽象路徑名和參數(shù)抽象路徑名是否相等,根據(jù)字典順序進(jìn)行比較
/* * 5. 比較抽象路徑名和參數(shù)抽象路徑名是否相等 * 比較兩個(gè)抽象的路徑名字典是否相等 等于零則相等,小于零則抽象路徑名字典小于參數(shù)路徑字典,大于則相反 * 比較規(guī)則按照字典順序進(jìn)行排序 */ String s1 = "C:/Boot"; // “D:/Demo” 與 "C:/Boot" 比較 System.out.println(f1.compareTo(new File(s1))); // expected output: 1 String s2 = "D:/Deoo"; // “D:/Demo” 與 "D:/Deoo" 比較 System.out.println(f1.compareTo(new File(s2))); // expected output: -2
結(jié)果:
6、創(chuàng)建一個(gè)新文件
/* * 6. 創(chuàng)建一個(gè)新文件 * 當(dāng)且僅當(dāng)具有該名稱的文件尚不存在時(shí),原子地創(chuàng)建一個(gè)由該抽象路徑名命名的新的空文件 * true: 如果命名文件不存在并被成功創(chuàng)建; * false: 如果命名文件已經(jīng)存在 */ File f3 = new File("/Boot"); try { System.out.println("/Boot file is created? " + f3.createNewFile()); // expected output: /Boot file is created? false } catch (IOException e) { e.printStackTrace(); }
結(jié)果:
7、創(chuàng)建一個(gè)目錄
/* * 7. 創(chuàng)建一個(gè)目錄 * 創(chuàng)建由此抽象路徑名命名的目錄 */ String dirnames = "D:/tmp/boot"; File f4 = new File(dirnames); // 創(chuàng)建一個(gè)文件夾,成功則返回true,失敗則返回false。 // 失敗表明File對(duì)象指定的路徑已經(jīng)存在,或者由于整個(gè)路徑還不存在,該文件夾不能被創(chuàng)建。 System.out.println("create mkdir is " + f4.mkdir()); // expected output: create mkdir is true
結(jié)果:
8、創(chuàng)建一個(gè)目錄,包括其不存在的父級(jí)目錄,因?yàn)樵谏狭兄袆?chuàng)建了對(duì)應(yīng)的目錄文件,所有mkdirs創(chuàng)建就返還false
/* * 8. 創(chuàng)建一個(gè)目錄,包括其不存在的父級(jí)目錄 * 創(chuàng)建一個(gè)文件夾和它的所有父文件夾 失敗表明File對(duì)象指定的路徑已經(jīng)存在 */ System.out.println("create mkdirs is " + f4.mkdirs()); // expected output: create mkdirs is false
9、刪除文件或者目錄(刪除前面創(chuàng)建的/tmp路徑下的boot)
/* * 9. 刪除文件或者目錄 * 刪除由此抽象路徑名表示的文件或目錄 * true當(dāng)且僅當(dāng)文件或目錄被成功刪除時(shí); false否則 */ System.out.println(dirnames + " deleted is " + f4.delete()); // expected output: D:/tmp/boot deleted is true
結(jié)果:
10、取得抽象路徑的名稱
/* * 10. 取得抽象路徑的名稱 * 取到抽象路徑名表示的文件或目錄的名稱 */ System.out.println("getName is " + f1.getName()); // expected output: getName is Demo
結(jié)果:
11、取得抽象路徑的字符串
/* * 11. 取得抽象路徑的字符串 * 獲得由抽象路徑名轉(zhuǎn)換為路徑名字符串 */ System.out.println("getPath is " + f1.getPath()); // expected output: getPath is D:\Demo
結(jié)果:
12、取得抽象路徑的絕對(duì)路徑
/* * 12. 取得抽象路徑的絕對(duì)路徑 * 獲得此抽象路徑名的絕對(duì)路徑名字符串 */ System.out.println("Absolute Path is " + f1.getAbsolutePath()); // expected output: Absolute Path is D:\Demo
結(jié)果:
13、判斷抽象路徑指定的文件或目錄是否存在
/* * 13. 判斷抽象路徑指定的文件或目錄是否存在 * 測(cè)試此抽象路徑名表示的文件或目錄是否存在 * true: 當(dāng)且僅當(dāng)存在由此抽象路徑名表示的文件或目錄時(shí); * false: 與true相反 */ System.out.println(f1.exists() ? "exist" : "not"); // expected output: exist
結(jié)果:
到此這篇關(guān)于Java File類簡(jiǎn)單使用(創(chuàng)建、刪除、遍歷與判斷是否存在等)的文章就介紹到這了,更多相關(guān)Java File類簡(jiǎn)單使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springcloud gateway自定義斷言規(guī)則詳解,以后綴結(jié)尾進(jìn)行路由
這篇文章主要介紹了springcloud gateway自定義斷言規(guī)則詳解,以后綴結(jié)尾進(jìn)行路由,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot Session共享實(shí)現(xiàn)圖解
這篇文章主要介紹了SpringBoot Session共享實(shí)現(xiàn)圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01SpringBoot統(tǒng)一數(shù)據(jù)返回格式的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot統(tǒng)一數(shù)據(jù)返回格式,它提高了代碼的可維護(hù)性和一致性,并改善了客戶端與服務(wù)端之間的通信,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05SpringBoot2 JPA解決懶加載異常的問(wèn)題
這篇文章主要介紹了SpringBoot2 JPA解決懶加載異常的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java實(shí)現(xiàn)五子棋(附詳細(xì)源碼)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03