[java]代碼庫
public static boolean createFile(String destFileName) {
File file = new File(destFileName);
if (file.exists()) {
System.out.println("建立單個檔案" + destFileName + "失敗,目标檔案已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
System.out.println("建立單個檔案" + destFileName + "失敗,目标檔案不能為目錄!");
return false;
}
// 判斷目标檔案所在的目錄是否存在
if (!file.getParentFile().exists()) {
// 如果目标檔案所在的檔案夾不存在,則建立父檔案夾
System.out.println("目标檔案所在目錄不存在,準備建立它!");
if (!file.getParentFile().mkdirs()) {
System.out.println("建立目标檔案所在的目錄失敗!");