首页 技术 正文
技术 2022年11月14日
0 收藏 869 点赞 3,436 浏览 3652 个字
public class FileOperater {
// 验证字符串是否为正确路径名的正则表达式
private static String matches = "[A-Za-z]:\\\\[^:?\"><*]*"; // 通过 sPath.matches(matches) 方法的返回值判断是否正确
// sPath 为路径字符串
/**
* 根据路径删除指定的目录或文件,无论存在与否
*
* @param sPath
* 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public static boolean DeleteFolder(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(sPath);
} else { // 为目录时调用删除目录方法
return deleteDirectory(sPath);
}
}
} /**
* 删除单个文件
*
* @param sPath
* 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
} /**
* 删除目录(文件夹)以及目录下的文件
*
* @param sPath
* 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
// 删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
// 删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag)
break;
} // 删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;
}
}
if (!flag)
return false;
// 删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
} 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("创建目录文件所在的目录失败!");
return false;
}
}
// 创建目标文件
try {
if (file.createNewFile()) {
System.out.println("创建单个文件" + destFileName + "成功!");
return true;
} else {
System.out.println("创建单个文件" + destFileName + "失败!");
return false;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("创建单个文件" + destFileName + "失败!");
return false;
}
} public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if (dir.exists()) {
System.out.println("创建目录" + destDirName + "失败,目标目录已存在!");
return false;
}
if (!destDirName.endsWith(File.separator))
destDirName = destDirName + File.separator;
// 创建单个目录
if (dir.mkdirs()) {
System.out.println("创建目录" + destDirName + "成功!");
return true;
} else {
System.out.println("创建目录" + destDirName + "成功!");
return false;
}
} public static String createTempFile(String prefix, String suffix,
String dirName) {
File tempFile = null;
try {
if (dirName == null) {
// 在默认文件夹下创建临时文件
tempFile = File.createTempFile(prefix, suffix);
return tempFile.getCanonicalPath();
} else {
File dir = new File(dirName);
// 如果临时文件所在目录不存在,首先创建
if (!dir.exists()) {
if (!FileOperater.createDir(dirName)) {
System.out.println("创建临时文件失败,不能创建临时文件所在目录!");
return null;
}
}
tempFile = File.createTempFile(prefix, suffix, dir);
return tempFile.getCanonicalPath();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("创建临时文件失败" + e.getMessage());
return null;
}
} public static void main(String[] args) {
/*
FileOperater hfc = new FileOperater();
String path = "D:\\Abc\\123\\Ab1";
boolean result = createDir(path);
System.out.println(result);
path = "D:\\Abc\\124";
result = hfc.DeleteFolder(path);
System.out.println(result); // 创建目录
String dirName = "c:/test/test0/test1";
createDir(dirName);
// 创建文件
String fileName = dirName + "/test2/testFile.txt";
CreateFile(fileName);
// 创建临时文件
String prefix = "temp";
String suffix = ".txt";
for (int i = 0; i < 10; i++) {
System.out.println("创建了临时文件:"
+ createTempFile(prefix, suffix, dirName));
}*/
//deleteFile("c:/test/test0/test1/test2/testFile.txt");
//deleteDirectory("c:/test/");
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300