手機(jī)網(wǎng)站 建設(shè)注冊(cè)域名后如何建立網(wǎng)站
Java文件IO操作
- 一、File類
- 1.相對(duì)路徑和絕對(duì)路徑
- 2.路徑分隔符(同一路徑下、多個(gè)路徑下)
- 3.實(shí)例化
- 4.常見方法
一、File類
File類繼承自O(shè)bject類,實(shí)現(xiàn)了Serializable接口和Comparable接口;
File類屬于java.io包;
File類是文件或者文件目錄的一種抽象表示,即可使用File類的具體實(shí)例化對(duì)象表示文件或者文件目錄;
File類只能對(duì)文件或文件目錄進(jìn)行一個(gè)抽象表示,可對(duì)其屬性特征進(jìn)行一定的描述,而如果想要對(duì)文件內(nèi)容進(jìn)行讀寫則需要借助IO流(具體地,將File對(duì)象作為IO流構(gòu)造器參數(shù));
1.相對(duì)路徑和絕對(duì)路徑
基于參考系不同,文件路徑描述可分為相對(duì)路徑和絕對(duì)路徑
相對(duì)路徑:相對(duì)于該文件當(dāng)前位置的路徑,比如./hello.java表示當(dāng)前目錄下的hello.java文件;
絕對(duì)路徑:該文件在文件系統(tǒng)中的詳細(xì)存儲(chǔ)位置,比如C:\javase\hello.java,即包括文件所在根目錄的路徑;
2.路徑分隔符(同一路徑下、多個(gè)路徑下)
1)同一路徑下
需要注意,不同操作系統(tǒng)中路徑分隔符是不同的,比如Linux下路徑分隔符為斜杠/,而Windows系統(tǒng)中為反斜杠\,但在java語(yǔ)言中反斜杠用于特殊字符轉(zhuǎn)義,故在java中Windows系統(tǒng)的路徑分隔符為\\。由于Java主打“一次編譯,到處運(yùn)行”,故代碼編寫時(shí)若不統(tǒng)一路徑分隔符會(huì)導(dǎo)致在不同的操作系統(tǒng)上運(yùn)行程序時(shí)會(huì)出現(xiàn)異常。Java官方為解決該問題,在File類中提供一靜態(tài)屬性separator,自動(dòng)獲取當(dāng)前操作系統(tǒng)的路徑分隔符。
使用方法如下:
File.separator
舉例:
C:\\javase\\hello.java可在代碼中表示為:C:+File.separator+javase+File.separator+hello.java
File.separator為字符串類型,故使用“+”進(jìn)行字符串拼接操作,這種操作看起來比較繁瑣,故我們可直接統(tǒng)一使用斜杠/作為路徑分隔符即可。
2)多個(gè)路徑下
此外,為了方便分隔多個(gè)路徑,如“./xxx/yyy/c.txt;./xxx/yyy/d.txt”表示兩個(gè)路徑,File類提供靜態(tài)屬性pathSeparator進(jìn)行此操作;
3)輸出Windows系統(tǒng)中這四個(gè)屬性的值:
System.out.println(File.pathSeparator);
System.out.println(File.pathSeparatorChar);
System.out.println(File.separator);
System.out.println(File.separatorChar);
對(duì)應(yīng)輸出:
;
;
\
\
3.實(shí)例化
提供以下四種構(gòu)造器,用于創(chuàng)建File類對(duì)象:
1)File(String pathname)
pathname可為:
- 具體文件的相對(duì)路徑或絕對(duì)路徑;
- 文件目錄的相對(duì)路徑或絕對(duì)路徑;
需要注意,此處的相對(duì)路徑必須是相對(duì)于該項(xiàng)目根目錄而言,及src目錄的父目錄;
// 其實(shí)此時(shí)File對(duì)象是一個(gè)抽象的路徑表示// 使用路徑字符串表示文件路徑或文件目錄File f1 = new File("./hello.txt");System.out.println(f1);File f2 = new File("F:\\IDEA_WorkSpace\\JavaIO\\JavaIO\\src\\com\\northsmile\\javaio\\hello.txt");System.out.println(f2);File f3 = new File("../demo");System.out.println(f3);File f4 = new File("F:\\IDEA_WorkSpace\\JavaIO\\JavaIO\\src\\com\\northsmile\\demo");System.out.println(f4);
對(duì)應(yīng)輸出:
.\hello.txt
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\javaio\hello.txt
..\demo
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\demo
2)File(String parent,String child)
- parent為父目錄;
- child為父目錄下某子目錄或文件所在子目錄路徑;
// 使用父路徑(父目錄)及子路徑(子目錄/文件所在子目錄路徑)構(gòu)建File f5 = new File("F:\\IDEA_WorkSpace\\JavaIO\\JavaIO\\src\\com", "northsmile");System.out.println(f5);File f6 = new File("F:\\IDEA_WorkSpace\\JavaIO\\JavaIO\\src\\com\\northsmile\\javaio", "hello.txt");System.out.println(f6);
對(duì)應(yīng)輸出:
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\javaio\hello.txt
3)File(File parent,String child)
- parent為File類對(duì)象表示的父目錄;
- child為父目錄下某子目錄或文件所在子目錄路徑;
// 使用File類對(duì)象表示父目錄,以此結(jié)合子路徑(子目錄/文件所在子目錄路徑)構(gòu)建File f7 = new File(f5, "javaio/hello.txt");System.out.println(f7);File f8 = new File(f5, "javaio");System.out.println(f8);
對(duì)應(yīng)輸出:
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\javaio\hello.txt
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\javaio
4.常見方法
方法簽名 | 含義 |
---|---|
public String getAbsolutePath() | 獲取絕對(duì)路徑 |
public String getPath() | 獲取路徑,如創(chuàng)建File對(duì)象時(shí)填寫的pathname |
public String getName() | 獲取文件名或文件目錄名 |
public String getParent() | 獲取當(dāng)前文件或目錄的上級(jí)目錄 |
public long length() | 獲取文件長(zhǎng)度,單位為字節(jié)數(shù);不能用于獲取目錄的長(zhǎng)度 |
public long lastModified() | 獲該文件或目錄最近一次修改時(shí)間,單位為毫秒 |
public String[] list() | 獲取指定目錄下所有文件名或子目錄的名稱數(shù)組(只有文件名或子目錄名) |
public File[] listFiles() | 獲取指定目錄下所有文件名或子目錄的File數(shù)組(路徑名均表示為絕對(duì)路徑形式) |
public boolean renameTo(File dest) | 將File對(duì)象表示的路徑重命名 |
public boolean isDirectory() | 是否為目錄 |
public boolean isFile() | 是否為文件 |
public boolean exists() | 指定路徑是否存在 |
public boolean canRead() | 指定路徑是否可讀 |
public boolean canWrite() | 指定路徑是否可寫 |
public boolean isHidden() | 是否為隱藏文件 |
public boolean createNewFile() | 如果指定文件不存在,則自動(dòng)創(chuàng)建該文件 |
public boolean mkdir() | 如果指定目錄不存在,則自動(dòng)創(chuàng)建指定目錄。前提是該目錄的父目錄存在。 |
public boolean mkdirs() | 如果指定目錄不存在,則自動(dòng)創(chuàng)建指定目錄。如果其父目錄不存在,會(huì)自動(dòng)為其創(chuàng)建父目錄 |
public boolean delete() | 刪除指定文件或目錄(只能刪除空目錄) |
示例1:
如果指定路徑在硬盤中真實(shí)存在,則在創(chuàng)建File對(duì)象時(shí),則會(huì)對(duì)其屬性進(jìn)行具體賦值,比如文件長(zhǎng)度等;反之如果不存在,則保持默認(rèn)值。
// 針對(duì)文件
File file = new File("F:\\IDEA_WorkSpace\\JavaIO\\JavaIO\\src\\com\\northsmile\\javaio\\hello.txt");
System.out.println(file.getAbsolutePath());
System.out.println(file.getPath());
System.out.println(file.getName());
System.out.println(file.getParent());
System.out.println(file.length());
System.out.println(file.lastModified());System.out.println("---------------------------");
// 針對(duì)文件目錄
File dir = new File("F:\\IDEA_WorkSpace\\JavaIO\\JavaIO\\src\\com\\northsmile");
System.out.println(dir.getAbsolutePath());
System.out.println(dir.getPath());
System.out.println(dir.getName());
System.out.println(dir.getParent());
System.out.println(dir.lastModified());
System.out.println("---------------------------");
String[] fileNames=dir.list();
for (String name:fileNames){System.out.println(name);
}
System.out.println("---------------------------");
File[] files=dir.listFiles();
for (File f:files){System.out.println(f);
}
輸出結(jié)果:
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\javaio\hello.txt
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\javaio\hello.txt
hello.txt
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\javaio
26
1677138552240
---------------------------
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile
northsmile
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com
1677138472477
---------------------------
demo
javaio
---------------------------
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\demo
F:\IDEA_WorkSpace\JavaIO\JavaIO\src\com\northsmile\javaioProcess finished with exit code 0
示例2:
// 重命名
File file1=new File("hello.txt");
File file2=new File("hi.txt");
System.out.println(file1.renameTo(file2));
renameTo表示將指定路徑重命名為目標(biāo)路徑,重命名成功有兩個(gè)前提:
- 源路徑存在,目標(biāo)路徑不存在;
- 源路徑-目標(biāo)路徑有兩種對(duì)應(yīng)形式:文件-文件或文件目錄-文件目錄,即可對(duì)文件或文件目錄進(jìn)行重命名;
示例3:
File file=new File("hello.txt");System.out.println(file.isDirectory());System.out.println(file.isFile());System.out.println(file.exists());System.out.println(file.canRead());System.out.println(file.canWrite());System.out.println(file.isHidden());
輸出結(jié)果:
false
true
true
true
true
false
示例4:
File file=new File("hi.txt");if (!file.exists()){ // 如果該文件不存在System.out.println("文件創(chuàng)建成功:"+file.createNewFile());}else{System.out.println("文件已存在,即將刪除該文件。");System.out.println("刪除成功:"+file.delete());}
File dir = new File("./testMkdir/demo");
if (!dir.exists()){if (dir.mkdir()){System.out.println("目錄創(chuàng)建成功");}else{System.out.println("目錄創(chuàng)建失敗");}}else{System.out.println("刪除該空目錄:"+dir.delete());
}
File dir = new File("./testMkdir/demo");if (!dir.exists()){if (dir.mkdirs()){System.out.println("目錄創(chuàng)建成功");}else{System.out.println("目錄創(chuàng)建失敗");}}else{System.out.println("刪除該空目錄:"+dir.delete());}