Java实例-目录操作
1、Java 实例 - 递归创建目录:使用 File 类的 mkdirs() 实现递归创建目录。
public class Contents_Create {
public Contents_Create(){
String directories ="D:\\a\\b";
File file =new File(directories);
boolean result = file.mkdirs();
System.out.println("Status = " + result);
}
}
运行结果:


2、Java 实例 - 删除目录:使用 File 类的 ofdir.isDirectory(), dir.list() 和 deleteDir() 方法在一个个删除文件后删除目录。
public class Contents_Delete {
public Contents_Delete()throws Exception {
// 删除当前目录下的 test 目录
deleteDir(new File("./test"));
}
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i =0; i < children.length; i++) {
boolean success =deleteDir
(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
if(dir.delete()) {
System.out.println("目录已被删除!");
return true;
}else {
System.out.println("目录删除失败!");
return false;
}
}
}
运行结果:


3、Java 实例 - 判断目录是否为空:使用 File 类的 file.isDirectory() 和 file.list() 方法来判断目录是否为空。
public class Contents_Exists {
public Contents_Exists(){
File file =new File("./test");// 当前目录下的 testdir目录
if(file.isDirectory()){
if(file.list().length>0){
System.out.println("目录不为空!");
}else{
System.out.println("目录为空!");
}
}else{
System.out.println("不存在该目录!");
}
}
}
运行结果:



4、Java 实例 - 判断文件是否隐藏:使用 File 类的 file.isHidden() 方法来判断文件是否隐藏。
public class Contents_Hide {
public Contents_Hide() {
File file =new File("test.txt");
System.out.println(file.isHidden());
}
}
运行结果:

5、Java 实例 - 获取目录大小:使用 File 类的 FileUtils.sizeofDirectory(File Name) 来获取目录的大小。
public class Contents_GetSize {
public Contents_GetSize() {
long size = FileUtils.sizeOfDirectory(new File("test"));
System.out.println("Size: " + size +" bytes");
}
}
运行结果:

6、Java 实例 - 在指定目录中查看文件:使用 File 类的 dir.list() 方法在指定目录中查找所有文件列表。
public class Contents_LookUp {
public Contents_LookUp()throws Exception {
File dir =new File("commons-io-2.6");
String[] children = dir.list();
if (children ==null) {
System.out.println("该目录不存在!");
}
else {
for (int i =0; i < children.length; i++) {
String filename = children[i];
System.out.println(filename);
}
}
}
}
运行结果:


7、Java 实例 - 获取文件的上级目录:使用 File 类的 file.getParent() 方法来获取文件的上级目录。
public class Contents_GetLast {
public Contents_GetLast() {
File file =new File("commons-io-2.6/commons-io-2.6.jar"); //建议写入绝对路径
String strParentDirectory = file.getParent();
System.out.println("文件的上级目录为 : " + strParentDirectory);
}
}
运行结果:

8、Java 实例 - 获取目录最后修改时间:使用 File 类的 file.lastModified() 方法来获取目录的最后修改时间。
public class Contents_GetTime {
public Contents_GetTime() {
File file =new File("commons-io-2.6/commons-io-2.6.jar");
System.out.println("最后修改时间:" +new Date(file.lastModified()));
}
}
运行结果:

9、Java 实例 - 打印目录结构:使用 File 类的 file.getName() 和 file.listFiles() 方法来打印目录结构。
public class Contents_Print {
public Contents_Print()throws IOException {
showDir(1,new File("./src"));
}
static void showDir(int indent, File file)throws IOException {
for (int i =0; i < indent; i++)
System.out.print('-');
System.out.println(file.getName());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i =0; i < files.length; i++)
showDir(indent +4, files[i]);
}
}
}
运行结果:

10、Java 实例 - 遍历指定目录下的所有目录:
public class Contents_Ergodic {
public Contents_Ergodic()throws IOException {
File dir =new File("out");
File[] files = dir.listFiles();
FileFilter fileFilter =new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter);
System.out.println(files.length);
if (files.length ==0) {
System.out.println("目录不存在或它不是一个目录");
}
else {
for (int i=0; i< files.length; i++) {
File filename = files[i];
System.out.println(filename.toString());
}
}
}
}
运行结果:


11、Java 实例 - 遍历指定目录下的所有文件:使用 File 类的 list 方法来输出指定目录下的所有文件。
public class Contents_ErgodicFiles {
public Contents_ErgodicFiles() {
File dir =new File("./src");
String[] children = dir.list();
if (children ==null) {
System.out.println("目录不存在或它不是一个目录");
}
else {
for (int i=0; i< children.length; i++) {
String filename = children[i];
System.out.println(filename);
}
}
}
}
运行结果:

12、Java 实例 - 在指定目录中查找文件:在 src目录下查找以字母 'C' 开头的所有文件。
public class Contents_FindFiles {
public Contents_FindFiles() {
File dir =new File("./src");
FilenameFilter filter =new FilenameFilter() {
public boolean accept
(File dir, String name) {
return name.startsWith("C");
}
};
String[] children = dir.list(filter);
if (children ==null) {
System.out.println("目录不存在或它不是一个目录");
}
else {
for (int i=0; i < children.length; i++) {
String filename = children[i];
System.out.println(filename);
}
}
}
}
运行结果:

13、Java 实例 - 遍历系统根目录:使用 File 类的 listRoots() 方法来输出系统所有根目录。
public class Contents_ErgodicRoot {
public Contents_ErgodicRoot(){
File[] roots = File.listRoots();
System.out.println("系统所有根目录:");
for (int i=0; i < roots.length; i++) {
System.out.println(roots[i].toString());
}
}
}
运行结果:

14、Java 实例 - 查看当前工作目录:使用 System 的 getProperty() 方法来获取当前的工作目录。
public class Contents_NowWork {
public Contents_NowWork() {
String curDir = System.getProperty("user.dir");
System.out.println("当前的工作目录为 :" + curDir);
}
}
运行结果:

15、Java 实例 - 遍历目录:使用 File 类的 dir.isDirectory() 和 dir.list() 方法来遍历目录。
public class Contents_Visit {
public Contents_Visit()throws Exception {
System.out.println("遍历目录");
File dir =new File("./out");//要遍历的目录
visitAllDirsAndFiles(dir);
}
public static void visitAllDirsAndFiles(File dir) {
System.out.println(dir);
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i =0; i < children.length; i++) {
visitAllDirsAndFiles(new File(dir, children[i]));
}
}
}
}
运行结果:
