Java文件操作之IO流 File類的使用詳解
File類概述
File類能新建、刪除、重命名文件和目錄,但不能訪問(wèn)文件內(nèi)容本身,如果需要訪問(wèn)文件內(nèi)容本身,則需要使用后續(xù)的輸入/輸出流。
要在Java程序中表示一個(gè)真實(shí)存在的文件或目錄,那么必須有一個(gè)File對(duì)象,但是Java程序中的一個(gè)File對(duì)象,可能沒(méi)有一個(gè)真實(shí)存在的文件或目錄。
File對(duì)象可以作為參數(shù)傳遞給流的構(gòu)造器。
常用構(gòu)造器
①public File(String pathname)
以pathname為路徑創(chuàng)建File對(duì)象,可以是絕對(duì)路徑或者相對(duì)路徑,如果是相對(duì)路徑,則默認(rèn)相對(duì)于當(dāng)前project。
File file1 = new File("hello.txt"); //相對(duì)路徑 File file2 = new File("C:\\IDEA\\untitled\\file\\hi.txt"); //絕對(duì)路徑
②public File(String parent,String child)
以parent為父路徑,child為子路徑創(chuàng)建File對(duì)象。
File file3 = new File("C:\\IDEA\\untitled\\", "file");
③public File(File parent,String child)
根據(jù)一個(gè)父File對(duì)象和子文件路徑創(chuàng)建File對(duì)象
File file3 = new File("C:\\IDEA\\untitled\\", "file"); File file4 = new File(file3, "hi.txt");
常用方法
①File類的獲取功能
public String getAbsolutePath()
:獲取絕對(duì)路徑
public String getPath()
:獲取路徑
public String getName()
:獲取名稱
public String getParent()
:獲取上層文件目錄路徑,若無(wú),返回null
public long length()
:獲取文件長(zhǎng)度(即:字節(jié)數(shù)),不能獲取目錄的長(zhǎng)度
public long lastModified()
:獲取最后一次的修改時(shí)間,毫秒值
public String[] list()
:獲取指定目錄下的所有文件或者文件目錄的名稱數(shù)組
public File[] listFiles()
:獲取指定目錄下的所有文件或文件目錄構(gòu)成的數(shù)組
import java.io.File; /** * @Author: Yeman * @Date: 2021-09-24-21:50 * @Description: */ public class FileTest { public static void main(String[] args) { File file1 = new File("hello.txt"); File file2 = new File("C:\\IDEA\\untitled\\file\\hi.txt"); System.out.println(file1.getAbsolutePath()); System.out.println(file1.getPath()); System.out.println(file1.getName()); System.out.println(file1.getParent()); System.out.println(file1.length()); System.out.println(file1.lastModified()); System.out.println(file2.getAbsolutePath()); System.out.println(file2.getPath()); System.out.println(file2.getName()); System.out.println(file2.getParent()); System.out.println(file2.length()); System.out.println(file2.lastModified()); } }
②File類的重命名功能
public boolean renameTo(File dest)
:把文件重命名為指定的文件路徑和文件名,相當(dāng)于是把真實(shí)文件轉(zhuǎn)移并且重命名了
import java.io.File; /** * @Author: Yeman * @Date: 2021-09-24-21:50 * @Description: */ public class FileTest { public static void main(String[] args) { File file1 = new File("hello.txt"); //file1需要在硬盤中真實(shí)存在 File file2 = new File("C:\\IDEA\\hi.txt"); //在硬盤中不存在file2 boolean b = file1.renameTo(file2); System.out.println(b); } }
③File類的判斷功能
硬盤中要真實(shí)存在才能做出真實(shí)判斷
public boolean isDirectory()
:判斷是否是文件目錄
public boolean isFile()
:判斷是否是文件
public boolean exists()
:判斷是否存在
public boolean canRead()
:判斷是否可讀
public boolean canWrite()
:判斷是否可寫
public boolean isHidden()
:判斷是否隱藏
④File類的創(chuàng)建功能
public boolean createNewFile()
:創(chuàng)建文件,若文件存在,則不創(chuàng)建,返回false
public boolean mkdir()
:創(chuàng)建文件目錄,如果此文件目錄存在,就不創(chuàng)建了,如果此文件目錄的上層目錄不存在,也不創(chuàng)建
public boolean mkdirs()
:創(chuàng)建文件目錄,如果上層文件目錄不存在,一并創(chuàng)建
⑤File類的刪除功能
public boolean delete()
:刪除文件或者文件夾
刪除注意事項(xiàng):Java中的刪除不走回收站。要?jiǎng)h除一個(gè)文件目錄,請(qǐng)注意該文件目錄內(nèi)不能包含文件或者文件目錄
import java.io.File; import java.io.IOException; /** * @Author: Yeman * @Date: 2021-09-24-21:50 * @Description: */ public class FileTest { public static void main(String[] args) throws IOException { File file1 = new File("hello.txt"); if (!file1.exists()){ //文件不存在,創(chuàng)建 file1.createNewFile(); System.out.println("創(chuàng)建成功"); }else { //文件存在,刪除 file1.delete(); System.out.println("刪除成功"); } } }
到此這篇關(guān)于Java文件操作之IO流 File類的使用詳解的文章就介紹到這了,更多相關(guān)Java file內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在RedHat系統(tǒng)上安裝JDK與Tomcat的步驟
這篇文章主要介紹了在RedHat系統(tǒng)上安裝Java與Tomcat的步驟,同樣適用于CentOS等RedHat系的Linux系統(tǒng),需要的朋友可以參考下2015-11-11springboot使用校驗(yàn)框架validation校驗(yàn)的示例
這篇文章主要介紹了springboot使用校驗(yàn)框架validation校驗(yàn)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02SpringBoot項(xiàng)目中控制臺(tái)日志的保存配置操作
這篇文章主要介紹了SpringBoot項(xiàng)目中控制臺(tái)日志的保存配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot集成Swagger2實(shí)現(xiàn)Restful(類型轉(zhuǎn)換錯(cuò)誤解決辦法)
這篇文章主要介紹了SpringBoot集成Swagger2實(shí)現(xiàn)Restful(類型轉(zhuǎn)換錯(cuò)誤解決辦法),需要的朋友可以參考下2017-07-07業(yè)務(wù)系統(tǒng)的Prometheus實(shí)踐示例詳解
這篇文章主要為大家介紹了業(yè)務(wù)系統(tǒng)的Prometheus實(shí)踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問(wèn)題
這篇文章主要介紹了解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07如何使用MybatisPlus的SQL注入器提升批量插入性能
本文給大家介紹如何使用MybatisPlus的SQL注入器提升批量插入性能,以實(shí)戰(zhàn)視角講述如何利用該特性提升MybatisPlus?的批量插入性能,感興趣的朋友跟隨小編一起看看吧2024-05-05