1. 程式人生 > 程式設計 >Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

前言

Java檔案類以抽象的方式代表檔名和目錄路徑名。該類本身不能用來讀資料或寫資料,它主要用於磁碟上檔案和目錄的建立、檔案的查詢和檔案的刪除。做一些非讀寫方面的工作,比如看看檔案是否存在、是否可讀寫及遍歷檔案目錄等等。要想讀寫資料,必須和其它io流的類配合使用,比如FileInputStream、FileOutputStream等。File物件代表磁碟中實際存在的檔案和目錄,以下就通過一些簡單的列子介紹File的基本使用。

這是整個File簡單使用的程式碼:

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 // 例項化一個File物件
 15 File f1 = new File(dirname);
 16 
 17 // 1. 遍歷指定目錄之下的所有檔案
 18 // 判斷f1物件是否是一個目錄
 19 if (f1.isDirectory()) {
 20 System.out.println("Directory of " + dirname);
 21 String[] s = f1.list();
 22 // 遍歷s陣列,取出陣列中的元素進行判斷
 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 // 不是一個目錄
 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. 測試指定檔案是否可執行
 43 * 測試應用程式是否可以執行此抽象路徑名錶示的檔案
 44 * true: 當且僅當存在抽象路徑名,並允許應用程式執行該檔案時
 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. 測試指定檔案是否可讀取
 52 * 測試應用程式是否可以讀取由此抽象路徑名錶示的檔案
 53 * true: 當且僅當此抽象路徑名指定的檔案存在並可由應用程式讀取時;
 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. 測試指定檔案是否可寫
 61 * 測試應用程式是否可以修改由此抽象路徑名錶示的檔案
 62 * true: 當且僅當檔案系統實際上包含由該抽象路徑名錶示的檔案並且允許應用程式寫入該檔案時;
 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. 比較抽象路徑名和引數抽象路徑名是否相等
 70 * 比較兩個抽象的路徑名字典是否相等 等於零則相等,小於零則抽象路徑名字典小於引數路徑字典,大於則相反
 71 * 比較規則按照字典順序進行排序
 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. 建立一個新檔案
 85 * 當且僅當具有該名稱的檔案尚不存在時,原子地建立一個由該抽象路徑名命名的新的空檔案
 86 * true: 如果命名檔案不存在並被成功建立;
 87 * false: 如果命名檔案已經存在
 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. 建立一個目錄
 99 * 建立由此抽象路徑名命名的目錄
100 */
101 String dirnames = "D:/tmp/boot";
102 File f4 = new File(dirnames);
103 // 建立一個資料夾,成功則返回true,失敗則返回false。
104 // 失敗表明File物件指定的路徑已經存在,或者由於整個路徑還不存在,該資料夾不能被建立。
105 System.out.println("create mkdir is " + f4.mkdir());
106 // expected output: create mkdir is true
107 
108 
109 /*
110 * 8. 建立一個目錄,包括其不存在的父級目錄
111 * 建立一個資料夾和它的所有父資料夾 失敗表明File物件指定的路徑已經存在
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當且僅當檔案或目錄被成功刪除時; 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 * 獲得由抽象路徑名轉換為路徑名字串
137 */
138 System.out.println("getPath is " + f1.getPath());
139 // expected output: getPath is D:\Demo
140 
141 /*
142 * 12. 取得抽象路徑的絕對路徑
143 * 獲得此抽象路徑名的絕對路徑名字串
144 */
145 System.out.println("Absolute Path is " + f1.getAbsolutePath());
146 // expected output: Absolute Path is D:\Demo
147 
148 
149 /*
150 * 13. 判斷抽象路徑指定的檔案或目錄是否存在
151 * 測試此抽象路徑名錶示的檔案或目錄是否存在
152 * true: 當且僅當存在由此抽象路徑名錶示的檔案或目錄時;
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目錄的結果如下所示:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

示例程式碼:

String dirname = "D:/Demo";
 // 例項化一個File物件
 File f1 = new File(dirname);

 // 1. 遍歷指定目錄之下的所有檔案
 // 判斷f1物件是否是一個目錄
 if (f1.isDirectory()) {
  System.out.println("Directory of " + dirname);
  String[] s = f1.list();
  // 遍歷s陣列,取出陣列中的元素進行判斷
  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 {
  // 不是一個目錄
  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

輸出結果:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

2、測試指定檔案是否可執行

/*
  * 2. 測試指定檔案是否可執行
  * 測試應用程式是否可以執行此抽象路徑名錶示的檔案
  * true: 當且僅當存在抽象路徑名,並允許應用程式執行該檔案時
  */
 System.out.println(dirname + " allowed to execute? " + f1.canExecute());
 // expected output: D:/Demo allowed to execute? true

3、測試指定檔案是否可讀取

/*
  * 3. 測試指定檔案是否可讀取
  * 測試應用程式是否可以讀取由此抽象路徑名錶示的檔案
  * true: 當且僅當此抽象路徑名指定的檔案存在並可由應用程式讀取時;
  * false: 與true相反
  */
 System.out.println(dirname + " allowed to read? " + f1.canRead());
 // expected output: D:/Demo allowed to read? true

4、測試指定檔案是否可寫

/*
  * 4. 測試指定檔案是否可寫
  * 測試應用程式是否可以修改由此抽象路徑名錶示的檔案
  * true: 當且僅當檔案系統實際上包含由該抽象路徑名錶示的檔案並且允許應用程式寫入該檔案時;
  * false: 與true相反
  */
 System.out.println(dirname + " allowed to write? " + f1.canWrite());
 // expected output: D:/Demo allowed to write? true

樣例2、3、4的結果可參考Demo 的屬性

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

5、比較抽象路徑名和引數抽象路徑名是否相等,根據字典順序進行比較

/*
  * 5. 比較抽象路徑名和引數抽象路徑名是否相等
  * 比較兩個抽象的路徑名字典是否相等 等於零則相等,小於零則抽象路徑名字典小於引數路徑字典,大於則相反
  * 比較規則按照字典順序進行排序
  */
 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

結果:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

6、建立一個新檔案

/*
  * 6. 建立一個新檔案
  * 當且僅當具有該名稱的檔案尚不存在時,原子地建立一個由該抽象路徑名命名的新的空檔案
  * true: 如果命名檔案不存在並被成功建立;
  * false: 如果命名檔案已經存在
  */
 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();
 }

結果:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

7、建立一個目錄

/*
  * 7. 建立一個目錄
  * 建立由此抽象路徑名命名的目錄
  */
 String dirnames = "D:/tmp/boot";
 File f4 = new File(dirnames);
 // 建立一個資料夾,成功則返回true,失敗則返回false。
 // 失敗表明File物件指定的路徑已經存在,或者由於整個路徑還不存在,該資料夾不能被建立。
 System.out.println("create mkdir is " + f4.mkdir());
 // expected output: create mkdir is true

結果:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

8、建立一個目錄,包括其不存在的父級目錄,因為在上列中建立了對應的目錄檔案,所有mkdirs建立就返還false

/*
 * 8. 建立一個目錄,包括其不存在的父級目錄
 * 建立一個資料夾和它的所有父資料夾 失敗表明File物件指定的路徑已經存在
 */
System.out.println("create mkdirs is " + f4.mkdirs());
// expected output: create mkdirs is false

9、刪除檔案或者目錄(刪除前面建立的/tmp路徑下的boot)

/*
  * 9. 刪除檔案或者目錄
  * 刪除由此抽象路徑名錶示的檔案或目錄
  * true當且僅當檔案或目錄被成功刪除時; false否則
  */
 System.out.println(dirnames + " deleted is " + f4.delete());
 // expected output: D:/tmp/boot deleted is true

結果:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

10、取得抽象路徑的名稱

/*
  * 10. 取得抽象路徑的名稱
  * 取到抽象路徑名錶示的檔案或目錄的名稱
  */
 System.out.println("getName is " + f1.getName());
 // expected output: getName is Demo

結果:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

11、取得抽象路徑的字串

/*
  * 11. 取得抽象路徑的字串
  * 獲得由抽象路徑名轉換為路徑名字串
  */
 System.out.println("getPath is " + f1.getPath());
 // expected output: getPath is D:\Demo

結果:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

12、取得抽象路徑的絕對路徑

/*
  * 12. 取得抽象路徑的絕對路徑
  * 獲得此抽象路徑名的絕對路徑名字串
  */
 System.out.println("Absolute Path is " + f1.getAbsolutePath());
 // expected output: Absolute Path is D:\Demo

結果:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

13、判斷抽象路徑指定的檔案或目錄是否存在

/*
  * 13. 判斷抽象路徑指定的檔案或目錄是否存在
  * 測試此抽象路徑名錶示的檔案或目錄是否存在
  * true: 當且僅當存在由此抽象路徑名錶示的檔案或目錄時;
  * false: 與true相反
  */
 System.out.println(f1.exists() ? "exist" : "not");
 // expected output: exist

結果:

Java File類的簡單使用教程(建立、刪除、遍歷與判斷是否存在等)

到此這篇關於Java File類簡單使用(建立、刪除、遍歷與判斷是否存在等)的文章就介紹到這了,更多相關Java File類簡單使用內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!