Skip to content

Instantly share code, notes, and snippets.

@dzwillpower
Created July 30, 2014 01:29
Show Gist options
  • Save dzwillpower/32a7510f1f72b9567fa0 to your computer and use it in GitHub Desktop.
Save dzwillpower/32a7510f1f72b9567fa0 to your computer and use it in GitHub Desktop.
创建文件夹 和递归删除文件夹
package com.wits.dzwillpower;
import java.io.File;
public class DirectoryCreate {
public static void main(String[] args) {
deleteFile(new File("E:\\ProgrammeMaterial\\Android\\AndroidProgram\\3372\\1\\1"));
}
public static void deleteFile(File file) {
File[] temp = file.listFiles();
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i].getName());
if (temp[i].isDirectory()) {
if (temp[i].listFiles().length != 0)
deleteFile(temp[i]);
deleteDir(temp[i]);
} else {
temp[i].delete();
}
}
}
private static void deleteDir(File file) {
if (file.listFiles().length == 0)
file.getAbsoluteFile().delete();
}
}
package com.oppo.java;
import java.io.File;
public class RecursionCreateDir {
public static void main(String[] args) {
File rootFile;
File files = new File("E:\\A\\B\\C");
rootFile = files;
if (!files.exists()) {
if (files.mkdirs()) {
System.out.println("创建多个目录成功");
} else {
System.out.println("创建多个目录失败.....");
}
}
for (int i = 0; i < 100; i++) {
File file = new File(rootFile, String.valueOf(i));
rootFile = file;
System.out.println("rootFile: " + rootFile);
if (!rootFile.exists()) {
if (rootFile.mkdir()) {
System.out.println("创建子目录成功");
} else {
System.out.println("创建子目录失败");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment