Commit 8aaa4473 by guanguan1009

修改

parent c1cde5f9
...@@ -27,7 +27,7 @@ public class Pdf2Pptx { ...@@ -27,7 +27,7 @@ public class Pdf2Pptx {
List<String> allDirPathName = new ArrayList<>(); List<String> allDirPathName = new ArrayList<>();
FileUtil.getAllDirPathNameRe(Constant.SOURCE_PATH, allDirPathName); FileUtil.getAllDirPathNameRe(Constant.SOURCE_PATH, allDirPathName);
for (String delFolder : allDirPathName) { for (String delFolder : allDirPathName) {
if (delFolder(delFolder)) { if (FileUtil.delFolder(delFolder)) {
logger.info("删除文件夹:{}及其文件成功", delFolder); logger.info("删除文件夹:{}及其文件成功", delFolder);
} }
} }
...@@ -90,57 +90,11 @@ public class Pdf2Pptx { ...@@ -90,57 +90,11 @@ public class Pdf2Pptx {
} }
//删除所有生成的图片文件 //删除所有生成的图片文件
for (String dirPathName : allDirPathName) { for (String dirPathName : allDirPathName) {
if (delFolder(dirPathName)) { if (dirPathName.endsWith("_图片") && FileUtil.delFolder(dirPathName)) {
logger.info("删除文件夹:{}及其文件成功", dirPathName); logger.info("删除文件夹:{}及其文件成功", dirPathName);
} }
} }
} }
//删除指定文件夹下所有文件
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
//删除文件夹
//param folderPath 文件夹完整绝对路径
public static boolean delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
return myFilePath.delete(); //删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
} }
...@@ -199,6 +199,53 @@ public class FileUtil { ...@@ -199,6 +199,53 @@ public class FileUtil {
return dirNameList; return dirNameList;
} }
//删除指定文件夹下所有文件
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
//删除文件夹
//param folderPath 文件夹完整绝对路径
public static boolean delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
return myFilePath.delete(); //删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) { public static void main(String[] args) {
List<String> allDirPathName = getAllDirPathName("F:\\pdf图片"); List<String> allDirPathName = getAllDirPathName("F:\\pdf图片");
for (String s : allDirPathName) { for (String s : allDirPathName) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment