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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java目录实用工具_JAVA 创建文件和文件夹,删除文件和文件夹的实用工具

發(fā)布時間:2025/3/11 编程问答 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java目录实用工具_JAVA 创建文件和文件夹,删除文件和文件夹的实用工具 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

package com.file;

import java.io.File;

import java.io.IOException;

//創(chuàng)建新文件和目錄

public class CCRDFile {

// 驗證字符串是否為正確路徑名的正則表達式

private static String matches = "[A-Za-z]:\\\\[^:?\">

// 通過 sPath.matches(matches) 方法的返回值判斷是否正確

// sPath 為路徑字符串

boolean flag = false;

File file;

public boolean DeleteFolder(String deletePath) {// 根據(jù)路徑刪除指定的目錄或文件,無論存在與否

flag = false;

if (deletePath.matches(matches)) {

file = new File(deletePath);

if (!file.exists()) {// 判斷目錄或文件是否存在

return flag; // 不存在返回 false

} else {

if (file.isFile()) {// 判斷是否為文件

return deleteFile(deletePath);// 為文件時調(diào)用刪除文件方法

} else {

return deleteDirectory(deletePath);// 為目錄時調(diào)用刪除目錄方法

}

}

} else {

System.out.println("要傳入正確路徑!");

return false;

}

}

public boolean deleteFile(String filePath) {// 刪除單個文件

flag = false;

file = new File(filePath);

if (file.isFile() && file.exists()) {// 路徑為文件且不為空則進行刪除

file.delete();// 文件刪除

flag = true;

}

return flag;

}

public boolean deleteDirectory(String dirPath) {// 刪除目錄(文件夾)以及目錄下的文件

// 如果sPath不以文件分隔符結(jié)尾,自動添加文件分隔符

if (!dirPath.endsWith(File.separator)) {

dirPath = dirPath + File.separator;

}

File dirFile = new File(dirPath);

// 如果dir對應(yīng)的文件不存在,或者不是一個目錄,則退出

if (!dirFile.exists() || !dirFile.isDirectory()) {

return false;

}

flag = true;

File[] files = dirFile.listFiles();// 獲得傳入路徑下的所有文件

for (int i = 0; i < files.length; i++) {// 循環(huán)遍歷刪除文件夾下的所有文件(包括子目錄)

if (files[i].isFile()) {// 刪除子文件

flag = deleteFile(files[i].getAbsolutePath());

System.out.println(files[i].getAbsolutePath() + " 刪除成功");

if (!flag)

break;// 如果刪除失敗,則跳出

} else {// 運用遞歸,刪除子目錄

flag = deleteDirectory(files[i].getAbsolutePath());

if (!flag)

break;// 如果刪除失敗,則跳出

}

}

if (!flag)

return false;

if (dirFile.delete()) {// 刪除當(dāng)前目錄

return true;

} else {

return false;

}

}

// 創(chuàng)建單個文件

public static boolean createFile(String filePath) {

File file = new File(filePath);

if (file.exists()) {// 判斷文件是否存在

System.out.println("目標(biāo)文件已存在" + filePath);

return false;

}

if (filePath.endsWith(File.separator)) {// 判斷文件是否為目錄

System.out.println("目標(biāo)文件不能為目錄!");

return false;

}

if (!file.getParentFile().exists()) {// 判斷目標(biāo)文件所在的目錄是否存在

// 如果目標(biāo)文件所在的文件夾不存在,則創(chuàng)建父文件夾

System.out.println("目標(biāo)文件所在目錄不存在,準(zhǔn)備創(chuàng)建它!");

if (!file.getParentFile().mkdirs()) {// 判斷創(chuàng)建目錄是否成功

System.out.println("創(chuàng)建目標(biāo)文件所在的目錄失敗!");

return false;

}

}

try {

if (file.createNewFile()) {// 創(chuàng)建目標(biāo)文件

System.out.println("創(chuàng)建文件成功:" + filePath);

return true;

} else {

System.out.println("創(chuàng)建文件失敗!");

return false;

}

} catch (IOException e) {// 捕獲異常

e.printStackTrace();

System.out.println("創(chuàng)建文件失敗!" + e.getMessage());

return false;

}

}

// 創(chuàng)建目錄

public static boolean createDir(String destDirName) {

File dir = new File(destDirName);

if (dir.exists()) {// 判斷目錄是否存在

System.out.println("創(chuàng)建目錄失敗,目標(biāo)目錄已存在!");

return false;

}

if (!destDirName.endsWith(File.separator)) {// 結(jié)尾是否以"/"結(jié)束

destDirName = destDirName + File.separator;

}

if (dir.mkdirs()) {// 創(chuàng)建目標(biāo)目錄

System.out.println("創(chuàng)建目錄成功!" + destDirName);

return true;

} else {

System.out.println("創(chuàng)建目錄失敗!");

return false;

}

}

// 創(chuàng)建臨時文件

public static String createTempFile(String prefix, String suffix,

String dirName) {

File tempFile = null;

if (dirName == null) {// 目錄如果為空

try {

tempFile = File.createTempFile(prefix, suffix);// 在默認文件夾下創(chuàng)建臨時文件

return tempFile.getCanonicalPath();// 返回臨時文件的路徑

} catch (IOException e) {// 捕獲異常

e.printStackTrace();

System.out.println("創(chuàng)建臨時文件失敗:" + e.getMessage());

return null;

}

} else {

// 指定目錄存在

File dir = new File(dirName);// 創(chuàng)建目錄

if (!dir.exists()) {

// 如果目錄不存在則創(chuàng)建目錄

if (CCRDFile.createDir(dirName)) {

System.out.println("創(chuàng)建臨時文件失敗,不能創(chuàng)建臨時文件所在的目錄!");

return null;

}

}

try {

tempFile = File.createTempFile(prefix, suffix, dir);// 在指定目錄下創(chuàng)建臨時文件

return tempFile.getCanonicalPath();// 返回臨時文件的路徑

} catch (IOException e) {// 捕獲異常

e.printStackTrace();

System.out.println("創(chuàng)建臨時文件失敗!" + e.getMessage());

return null;

}

}

}

public static void main(String[] args) {

String dirName = "E:/createFile/";// 創(chuàng)建目錄

CCRDFile.createDir(dirName);// 調(diào)用方法創(chuàng)建目錄

String fileName = dirName + "/file1.txt";// 創(chuàng)建文件

CCRDFile.createFile(fileName);// 調(diào)用方法創(chuàng)建文件

String prefix = "temp";// 創(chuàng)建臨時文件

String surfix = ".txt";// 后綴

for (int i = 0; i < 10; i++) {// 循環(huán)創(chuàng)建多個文件

System.out.println("創(chuàng)建臨時文件: "// 調(diào)用方法創(chuàng)建臨時文件

+ CCRDFile.createTempFile(prefix, surfix,

dirName));

}

}

}

總結(jié)

以上是生活随笔為你收集整理的java目录实用工具_JAVA 创建文件和文件夹,删除文件和文件夹的实用工具的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。