Java判斷檔案目錄以及檔案是否存在

2021-05-26 21:10:26 字數 510 閱讀 5655

近日在專案中遇到乙個問題,需要在指定的目錄下面,找到指定的檔案。查詢相關方法後,特將方法寫出來以便日後用到。

* 判斷檔案及目錄是否存在,若不存在則建立檔案及目錄

*@paramfilepath

*@return

*@throwsexception

public staticfile checkexist(string filepath)throwsexceptionelseelseelse{      

file.createnewfile();//建立檔案 

system.out.println("檔案不存在,建立檔案成功!"   );      

returnfile;

if 判斷檔案 目錄屬性

f file 判斷是否是普通檔案,是否存在 bin bash f tmp 1.txt 定義變數 if f f 如果變數f存在,且是個普通檔案 then 那麼 echo f exist echo f exist else 否則 touch f 建立這個檔案 fi d file 判斷是否是目錄,是否存在...

linux c 判斷路徑是 目錄還是檔案

主要函式 stat opendir readdir 主要結構體 struct dirent 和 struct stat 主要巨集 s isdir s isreg 函式stat 用於獲取乙個檔案路徑的資訊,並把獲取到的資訊放到結構體 struct stat 中 函式 opendir 用於生成乙個目錄指...

刪除目錄(資料夾)以及目錄下的檔案

刪除目錄 資料夾 以及目錄下的檔案 param spath 被刪除目錄的檔案路徑 return 目錄刪除成功返回true,否則返回false public static boolean deletedirectory string spath file dirfile new file spath ...