Java 中的 File類詳情
一、File類簡(jiǎn)單介紹
為什么要學(xué)習(xí)File類呢,他的作用又是什么呢?
IO流操作中大部分都是對(duì)文件進(jìn)行操作,所以Java
就提供了一個(gè)File類供我們來操作文件,它是以抽象的方式代表文件名和目錄路徑名,該類主要是對(duì)文件或目錄的創(chuàng)建,文件的查找,刪除等。對(duì)于File而言,其封裝的并不是一個(gè)真正存在的文件,僅僅是一個(gè)路徑名而已。它可以是存在的,也可以是不存在的。將來是要通過具體的操作把這個(gè)路徑的內(nèi)容轉(zhuǎn)換為具體存在的。
二、 路徑的分類
- 絕對(duì)路徑:帶有盤符號(hào)的路徑
- 相對(duì)路徑:沒有帶盤符號(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){ //通過路徑得到對(duì)象 File file = new File("E:\\aaa\\"); //通過父類路徑和子類名得到對(duì)象 File file1 = new File("E:\\", "aaa"); //通過父類對(duì)象和子類名得到對(duì)象 File file2 = new File("E:\\"); File file3 = new File(file2, "aaa"); } }
四、 成員方法
創(chuàng)建功能:
public boolean createNewFile()
當(dā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():
判斷是否可寫
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():獲取文件或文件夾名稱
public long length():獲取長(zhǎng)度,字節(jié)數(shù),可以獲取文件的大小進(jìn)行判斷
public long lastModified():獲取最后一次修改的時(shí)間,返回毫秒值,可以判斷文件被修改過幾次
高級(jí)獲取功能:
public String[ ] list():獲取目錄下的所有文件或者文件夾的名稱數(shù)組
public File[ ] listFiles():獲取指定目錄下的所有文件夾的File對(duì)象數(shù)組,返回的是File對(duì)象說明可以調(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():返回由此抽象路徑名表示的文件或目錄的名稱 System.out.println(f.getAbsolutePath()); System.out.println(f.getPath()); System.out.println(f.getName()); System.out.println("--------"); // public String[] list():返回此抽象路徑名表示的目錄中的文件和目錄的名稱字符串?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()); } } } }
文件過濾接口:
想獲取的時(shí)候就滿足條件,要實(shí)現(xiàn)文件過濾接口:public String[ ] listFiles(new FilenameFilter)
到此這篇關(guān)于Java 中的 File類詳情的文章就介紹到這了,更多相關(guān)Java 中的 File類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳功能
這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06深入解析Java中的編碼轉(zhuǎn)換以及編碼和解碼操作
這篇文章主要介紹了Java中的編碼轉(zhuǎn)換以及編碼和解碼操作,文中詳細(xì)解讀了編碼解碼的相關(guān)IO操作以及內(nèi)存使用方面的知識(shí),需要的朋友可以參考下2016-02-02Java數(shù)組高級(jí)算法與Arrays類常見操作小結(jié)【排序、查找】
這篇文章主要介紹了Java數(shù)組高級(jí)算法與Arrays類常見操作,結(jié)合實(shí)例形式總結(jié)分析了Java數(shù)組常見的排序算法、查找算法相關(guān)原理、實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2019-03-03java實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳下載功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳下載功能的具體代碼,感興趣的小伙伴們可以參考一下2016-05-05iOS獲取AppIcon and LaunchImage''s name(app圖標(biāo)和啟動(dòng)圖片名字)
這篇文章主要介紹了iOS獲取AppIcon and LaunchImage's name(app圖標(biāo)和啟動(dòng)圖片名字)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-08-08Spring的@RequestParam對(duì)象綁定方式
這篇文章主要介紹了Spring的@RequestParam對(duì)象綁定方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10