日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 检查目录是否存在_如何检查Java目录是否存在?

發布時間:2023/12/1 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 检查目录是否存在_如何检查Java目录是否存在? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java 檢查目錄是否存在

  • We are using the File class that is an abstract representation of file and directory path. To check if a directory exists we have to follow a few steps:

    我們正在使用File類 ,它是文件和目錄路徑的抽象表示。 要檢查目錄是否存在,我們必須執行以下步驟:

  • Create a File object and at the time of instantiation, we have to give abstract path there for which we will be in searching.

    創建一個File對象 ,在實例化時,我們必須在其中提供抽象路徑,以便進行搜索。

  • By using exists() method of File. This method tests whether the directory exists. The return type of this method is boolean so it returns true if and only if the directory exists and otherwise it will return false.

    通過使用File的exist()方法。 此方法測試目錄是否存在。 此方法的返回類型為boolean,因此僅當目錄存在時,它才返回true,否則它將返回false。

  • We will understand clearly with the help of an example.

    我們將通過一個示例清楚地了解。

Example:

例:

import java.io.File;public class ToCheckDirectoryExists {public static void main(String[] args) {File dir_path1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");File dir_path2 = new File("C:\\Users\\computer clinic\\Articles");// By using exists() method of File will check whether the // specified directory exists or not and exist() method works // with File class object because of its File method and // it return Boolean return true if directory exists false otherwise.boolean dir_exists1 = dir_path1.exists();boolean dir_exists2 = dir_path2.exists();// By using getPath()method to retrieve the given path of the // directory and dir_exists1 and dir_exists1 returns true // when directory exists else false.System.out.println("Given Directory1 " + dir_path1.getPath() + " exists: " + dir_exists1);System.out.println("Given Directory2 " + dir_path2.getPath() + " is not exists: " + dir_exists2);} }

Output

輸出量

D:\Programs>javac ToCheckDirectoryExists.javaD:\Programs>java ToCheckDirectoryExists Given Directory1 C:\Users\computer clinic\OneDrive\Articles exists: true Given Directory2 C:\Users\computer clinic\Articles is not exists: false

翻譯自: https://www.includehelp.com/java/how-to-check-if-directory-exists-in-java.aspx

java 檢查目錄是否存在

總結

以上是生活随笔為你收集整理的java 检查目录是否存在_如何检查Java目录是否存在?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。