Java 中的 File類(lèi)詳情
一、File類(lèi)簡(jiǎn)單介紹
為什么要學(xué)習(xí)File類(lèi)呢,他的作用又是什么呢?
IO流操作中大部分都是對(duì)文件進(jìn)行操作,所以Java就提供了一個(gè)File類(lèi)供我們來(lái)操作文件,它是以抽象的方式代表文件名和目錄路徑名,該類(lèi)主要是對(duì)文件或目錄的創(chuàng)建,文件的查找,刪除等。對(duì)于File而言,其封裝的并不是一個(gè)真正存在的文件,僅僅是一個(gè)路徑名而已。它可以是存在的,也可以是不存在的。將來(lái)是要通過(guò)具體的操作把這個(gè)路徑的內(nèi)容轉(zhuǎn)換為具體存在的。
二、 路徑的分類(lèi)
- 絕對(duì)路徑:帶有盤(pán)符號(hào)的路徑
- 相對(duì)路徑:沒(méi)有帶盤(pán)符號(hào)的路徑,默認(rèn)在根目錄下
三、 構(gòu)造方法
File(String pathname):根據(jù)一個(gè)路徑得到File對(duì)象File(String parent, String child):根據(jù)一個(gè)目錄和一個(gè)子文件/目錄得到File對(duì)象File(File parent, String child):根據(jù)一個(gè)父File對(duì)象和子文件/目錄得到File對(duì)象
示例代碼:
package org.westos.demo2;
import java.io.File;
public class MyTest2 {
public static void main(String[] args){
//通過(guò)路徑得到對(duì)象
File file = new File("E:\\aaa\\");
//通過(guò)父類(lèi)路徑和子類(lèi)名得到對(duì)象
File file1 = new File("E:\\", "aaa");
//通過(guò)父類(lèi)對(duì)象和子類(lèi)名得到對(duì)象
File file2 = new File("E:\\");
File file3 = new File(file2, "aaa");
}
}
四、 成員方法
創(chuàng)建功能:
public boolean createNewFile()當(dāng)具有該名稱(chēng)的文件不存在時(shí),創(chuàng)建一個(gè)由該抽象路徑名命名的新空文件。
public boolean mkdir()創(chuàng)建由此抽象路徑名命名的目錄。 public boolean
mkdirs()創(chuàng)建由此抽象路徑名命名的目錄,包括任何必需但不存在的父目錄。
示例代碼:
public class FileDemo02 {
public static void main(String[] args) throws IOException {
//需求1:我要在E:\\itcast目錄下創(chuàng)建一個(gè)文件java.txt
File f1 = new File("E:\\itcast\\java.txt");
System.out.println(f1.createNewFile());
System.out.println("--------");
//需求2:我要在E:\\itcast目錄下創(chuàng)建一個(gè)目錄JavaSE
File f2 = new File("E:\\itcast\\JavaSE");
System.out.println(f2.mkdir());
System.out.println("--------");
//需求3:我要在E:\\itcast目錄下創(chuàng)建一個(gè)多級(jí)目錄JavaWEB\\HTML
File f3 = new File("E:\\itcast\\JavaWEB\\HTML");
// System.out.println(f3.mkdir());
System.out.println(f3.mkdirs());
System.out.println("--------");
//需求4:我要在E:\\itcast目錄下創(chuàng)建一個(gè)文件javase.txt
File f4 = new File("E:\\itcast\\javase.txt");
// System.out.println(f4.mkdir());
System.out.println(f4.createNewFile());
}
}
刪除功能:
public boolean delete() ;
注意:
- 要?jiǎng)h除一個(gè)文件夾,請(qǐng)注意該文件夾內(nèi)不能包含文件或文件夾
- java中刪除不走回收站
示例代碼:
public class FileDemo03 {
public static void main(String[] args) throws IOException {
// File f1 = new File("E:\\itcast\\java.txt");
//需求1:在當(dāng)前模塊目錄下創(chuàng)建java.txt文件
File f1 = new File("myFile\\java.txt");
// System.out.println(f1.createNewFile());
//需求2:刪除當(dāng)前模塊目錄下的java.txt文件
System.out.println(f1.delete());
System.out.println("--------");
//需求3:在當(dāng)前模塊目錄下創(chuàng)建itcast目錄
File f2 = new File("myFile\\itcast");
// System.out.println(f2.mkdir());
//需求4:刪除當(dāng)前模塊目錄下的itcast目錄
System.out.println(f2.delete());
System.out.println("--------");
//需求5:在當(dāng)前模塊下創(chuàng)建一個(gè)目錄itcast,然后在該目錄下創(chuàng)建一個(gè)文件java.txt
File f3 = new File("myFile\\itcast");
// System.out.println(f3.mkdir());
File f4 = new File("myFile\\itcast\\java.txt");
// System.out.println(f4.createNewFile());
//需求6:刪除當(dāng)前模塊下的目錄itcast
System.out.println(f4.delete());
System.out.println(f3.delete());
}
}
重命名功能:
public boolean renameTo(File dest):如果路徑名相同,就是改名;如果路徑名不相同,就是改名并剪切
判斷功能:
public boolean isDirectory():判斷是否是文件夾
public boolean isFile():判斷是否是文件
public boolean exists():判斷文件或文件夾是否存在
public boolean canRead(): 判斷是否可讀
public boolean canWrite(): 判斷是否可寫(xiě)
public boolean isHidden(): 判斷文件或文件夾是否隱藏
示例代碼:
public class FileDemo04 {
public static void main(String[] args) {
//創(chuàng)建一個(gè)File對(duì)象
File f = new File("myFile\\java.txt");
// public boolean isDirectory():測(cè)試此抽象路徑名表示的File是否為目錄
// public boolean isFile():測(cè)試此抽象路徑名表示的File是否為文件
// public boolean exists():測(cè)試此抽象路徑名表示的File是否存在
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(f.exists());
}
}
獲取功能:
基本獲取功能:
public String getAbsolutePath():獲取文件或文件夾的絕對(duì)路徑
public String getPath():獲取文件或文件夾的相對(duì)路徑
public String getName():獲取文件或文件夾名稱(chēng)
public long length():獲取長(zhǎng)度,字節(jié)數(shù),可以獲取文件的大小進(jìn)行判斷
public long lastModified():獲取最后一次修改的時(shí)間,返回毫秒值,可以判斷文件被修改過(guò)幾次
高級(jí)獲取功能:
public String[ ] list():獲取目錄下的所有文件或者文件夾的名稱(chēng)數(shù)組
public File[ ] listFiles():獲取指定目錄下的所有文件夾的File對(duì)象數(shù)組,返回的是File對(duì)象說(shuō)明可以調(diào)用File的方法
示例代碼:
public class FileDemo04 {
public static void main(String[] args) {
//創(chuàng)建一個(gè)File對(duì)象
File f = new File("myFile\\java.txt");
// public String getAbsolutePath():返回此抽象路徑名的絕對(duì)路徑名字符串
// public String getPath():將此抽象路徑名轉(zhuǎn)換為路徑名字符串
// public String getName():返回由此抽象路徑名表示的文件或目錄的名稱(chēng)
System.out.println(f.getAbsolutePath());
System.out.println(f.getPath());
System.out.println(f.getName());
System.out.println("--------");
// public String[] list():返回此抽象路徑名表示的目錄中的文件和目錄的名稱(chēng)字符串?dāng)?shù)組
// public File[] listFiles():返回此抽象路徑名表示的目錄中的文件和目錄的File對(duì)象數(shù)組
File f2 = new File("E:\\itcast");
String[] strArray = f2.list();
for(String str : strArray) {
System.out.println(str);
}
System.out.println("--------");
File[] fileArray = f2.listFiles();
for(File file : fileArray) {
// System.out.println(file);
// System.out.println(file.getName());
if(file.isFile()) {
System.out.println(file.getName());
}
}
}
}
文件過(guò)濾接口:
想獲取的時(shí)候就滿(mǎn)足條件,要實(shí)現(xiàn)文件過(guò)濾接口:public String[ ] listFiles(new FilenameFilter)
到此這篇關(guān)于Java 中的 File類(lèi)詳情的文章就介紹到這了,更多相關(guān)Java 中的 File類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中extends與implements的區(qū)別淺談
java中extends與implements的區(qū)別淺談,需要的朋友可以參考一下2013-03-03
Springboot如何使用filter對(duì)request body參數(shù)進(jìn)行校驗(yàn)
這篇文章主要介紹了Springboot如何使用filter對(duì)request body參數(shù)進(jìn)行校驗(yàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot之HandlerInterceptor攔截器的使用詳解
這篇文章主要介紹了SpringBoot之HandlerInterceptor攔截器的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Java多線(xiàn)程之顯示鎖和內(nèi)置鎖總結(jié)詳解
這篇文章主要介紹了Java多線(xiàn)程之顯示鎖和內(nèi)置鎖總結(jié)詳解,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Idea 解決 Could not autowire. No beans of ''xxxx'' type found
這篇文章主要介紹了Idea 解決 Could not autowire. No beans of 'xxxx' type found 的錯(cuò)誤提示,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
java中thread線(xiàn)程start和run的區(qū)別
這篇文章主要介紹了java中thread線(xiàn)程start和run的區(qū)別,run()是Runnable接口中定義的一個(gè)方法,是為了讓客戶(hù)程序員在這個(gè)方法里寫(xiě)自己的功能代碼的。直接調(diào)用和普通的類(lèi)調(diào)用自己的成員方法是沒(méi)有任何區(qū)別的2014-03-03
Web容器啟動(dòng)過(guò)程中如何執(zhí)行Java類(lèi)
這篇文章主要介紹了Web容器啟動(dòng)過(guò)程中如何執(zhí)行Java類(lèi),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10

