共計 472 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中可以使用 File 類的 exists() 方法和 isDirectory() 方法來判斷文件夾是否存在。
示例代碼如下:
import java.io.File;
public class CheckFolderExists {public static void main(String[] args) {String folderPath = "path/to/folder";
File folder = new File(folderPath);
if (folder.exists() && folder.isDirectory()) {System.out.println(" 文件夾存在!");
} else {System.out.println(" 文件夾不存在!");
}
}
}
在上面的示例代碼中,首先創建一個 File 對象來表示要判斷的文件夾,然后使用 exists() 方法和 isDirectory() 方法來判斷文件夾是否存在。如果 exists() 方法返回 true 并且 isDirectory() 方法也返回 true,則表示文件夾存在;否則表示文件夾不存在。
丸趣 TV 網 – 提供最優質的資源集合!
正文完