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