java目录实用工具_JAVA 创建文件和文件夹,删除文件和文件夹的实用工具
package com.file;
import java.io.File;
import java.io.IOException;
//創(chuàng)建新文件和目錄
public class CCRDFile {
// 驗(yàn)證字符串是否為正確路徑名的正則表達(dá)式
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);// 為文件時(shí)調(diào)用刪除文件方法
} else {
return deleteDirectory(deletePath);// 為目錄時(shí)調(diào)用刪除目錄方法
}
}
} else {
System.out.println("要傳入正確路徑!");
return false;
}
}
public boolean deleteFile(String filePath) {// 刪除單個(gè)文件
flag = false;
file = new File(filePath);
if (file.isFile() && file.exists()) {// 路徑為文件且不為空則進(jìn)行刪除
file.delete();// 文件刪除
flag = true;
}
return flag;
}
public boolean deleteDirectory(String dirPath) {// 刪除目錄(文件夾)以及目錄下的文件
// 如果sPath不以文件分隔符結(jié)尾,自動(dòng)添加文件分隔符
if (!dirPath.endsWith(File.separator)) {
dirPath = dirPath + File.separator;
}
File dirFile = new File(dirPath);
// 如果dir對應(yīng)的文件不存在,或者不是一個(gè)目錄,則退出
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 {// 運(yùn)用遞歸,刪除子目錄
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;// 如果刪除失敗,則跳出
}
}
if (!flag)
return false;
if (dirFile.delete()) {// 刪除當(dāng)前目錄
return true;
} else {
return false;
}
}
// 創(chuàng)建單個(gè)文件
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)建臨時(shí)文件
public static String createTempFile(String prefix, String suffix,
String dirName) {
File tempFile = null;
if (dirName == null) {// 目錄如果為空
try {
tempFile = File.createTempFile(prefix, suffix);// 在默認(rèn)文件夾下創(chuàng)建臨時(shí)文件
return tempFile.getCanonicalPath();// 返回臨時(shí)文件的路徑
} catch (IOException e) {// 捕獲異常
e.printStackTrace();
System.out.println("創(chuàng)建臨時(shí)文件失敗:" + 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)建臨時(shí)文件失敗,不能創(chuàng)建臨時(shí)文件所在的目錄!");
return null;
}
}
try {
tempFile = File.createTempFile(prefix, suffix, dir);// 在指定目錄下創(chuàng)建臨時(shí)文件
return tempFile.getCanonicalPath();// 返回臨時(shí)文件的路徑
} catch (IOException e) {// 捕獲異常
e.printStackTrace();
System.out.println("創(chuàng)建臨時(shí)文件失敗!" + 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)建臨時(shí)文件
String surfix = ".txt";// 后綴
for (int i = 0; i < 10; i++) {// 循環(huán)創(chuàng)建多個(gè)文件
System.out.println("創(chuàng)建臨時(shí)文件: "// 調(diào)用方法創(chuàng)建臨時(shí)文件
+ CCRDFile.createTempFile(prefix, surfix,
dirName));
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java目录实用工具_JAVA 创建文件和文件夹,删除文件和文件夹的实用工具的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL 服务无法启动--服务没有报告
- 下一篇: html 图片position,【图解C