Java文件操作之IO流 File類的使用詳解
File類概述
File類能新建、刪除、重命名文件和目錄,但不能訪問文件內容本身,如果需要訪問文件內容本身,則需要使用后續(xù)的輸入/輸出流。
要在Java程序中表示一個真實存在的文件或目錄,那么必須有一個File對象,但是Java程序中的一個File對象,可能沒有一個真實存在的文件或目錄。
File對象可以作為參數(shù)傳遞給流的構造器。
常用構造器
①public File(String pathname)
以pathname為路徑創(chuàng)建File對象,可以是絕對路徑或者相對路徑,如果是相對路徑,則默認相對于當前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()
:獲取指定目錄下的所有文件或文件目錄構成的數(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)
:把文件重命名為指定的文件路徑和文件名,相當于是把真實文件轉移并且重命名了
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中的刪除不走回收站。要刪除一個文件目錄,請注意該文件目錄內不能包含文件或者文件目錄
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("刪除成功"); } } }
到此這篇關于Java文件操作之IO流 File類的使用詳解的文章就介紹到這了,更多相關Java file內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Cloud Hystrix線程池不足的解決方法
這篇文章主要介紹了Spring Cloud Hystrix線程池不足的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02SpringBoot整合騰訊云COS對象存儲實現(xiàn)文件上傳的示例代碼
本文主要介紹了SpringBoot整合騰訊云COS對象存儲實現(xiàn)文件上傳的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12深入了解Spring Boot2.3.0及以上版本的Liveness和Readiness功能
這篇文章主要介紹了Spring Boot2.3.0及以上版本的Liveness和Readiness功能示例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10Spring Cloud 部署時使用 Kubernetes 作為注冊中心和配置中
Spring Cloud Kubernetes提供了使用Kubernete本地服務的Spring Cloud通用接口實現(xiàn),這篇文章主要介紹了Spring Cloud 部署時如何使用 Kubernetes 作為注冊中心和配置中心,需要的朋友可以參考下2024-05-05