JAVA——File類

一、File類的概念

An abstract representation of file and directory pathnames(它是文件和目錄路徑名的一個(gè)抽象的表現(xiàn)形式)

二、 File類的成員變量

? ??成員變量均為靜態(tài)常量,且能夠適配不同的操作系統(tǒng)

1? ? staticString?pathSeparator

? ? ? ? ? ?它表示與系統(tǒng)有關(guān)的路徑分隔符,為了方便,它被表示為一個(gè)字符串。?

2? ? ?staticchar?pathSeparatorChar

? ? ? ? ? ? 它表示與系統(tǒng)有關(guān)的路徑分隔符。?

3? ? ?static?String?separator

? ? ? ? ? ? ?它表示與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符,為了方便,它被表示為一個(gè)字符串。?

4? ? ?staticchar?separatorChar

? ? ? ? ? ? ?與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符。? ? ? ??

三、File類的構(gòu)造方法

????public?File(String?pathname)?????通過將給定的路徑名字符串轉(zhuǎn)換為抽象路徑名來創(chuàng)建新的File實(shí)例。

????public?File(String?parent,String?child)? ? ? ? ? ? ? 從父抽象路徑名和子路徑名字符串創(chuàng)建新的File實(shí)例。

????public?File(File?parent,String?child)? ? ? ? ? ? ? 從父路徑名字符串和子路徑名字符串創(chuàng)建新的File實(shí)例。

四、常用方法

? ?public boolean createNewFile()? 若文件不存在則創(chuàng)建新文件

? ?public boolean mkdir()? ? ? ? ? ? ? ? 創(chuàng)建目錄 make Directory

? ?public boolean mkdirs()? ? ? ? ? ? ? 創(chuàng)建目錄及不存在的父目錄

? ?public boolean delete() 刪除文件及空目錄

? ?public boolean renameTo(File dest)?

? ? a.? 如果源文件和目標(biāo)前父目錄一致,就是更名

? ? b.? 如果源文件和目標(biāo)前父目錄不一致,就是剪切再更名

? ?public String[] list() ????????????????返回file下的文件和目錄名稱(字符串)

? ?public File[] listFiles()? ????????????返回file下的文件和目錄(文件)

? ?public String[] list(FilenameFilter filter)? ????????根據(jù)filter過濾返回file下文件和目錄名稱

? ?public? File[] listFiles(FileFilter filter)? ????????根據(jù)filter過濾返回file下文件和目錄

? ?public?boolean?isFile()? ? ? ? ? ? ? ? ?是否是文件

? ?public?boolean?isDirectory()? ? ? ? 是否是目錄

? ?public?boolean?canRead()? ? ? ? ? ?是否可讀

? ?public?boolean?canWrite()? ? ? ? ? ?是否可寫

? ?public?boolean?exists()? ? ? ? ? ? ? ? 是否存在

? ?public?boolean?isHidden()? ? ? ? ? ?是否隱藏

? ?public?long?length()? ? ? ? ? ? ? ? ? ? ? 長度

? ?publicStringgetAbsolutePath()? ? ?絕對(duì)路徑

? ?public?String?getPath()? ? ? ? ? ? ? ? ?定義的路徑

? ?public?String?getName()? ? ? ? ? ? ? ?獲取file名字

? ?public?long?lastModified()? ? ? ? ? ? ?最后一次修改時(shí)間

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容