首页 > 其他 > 详细

目录操作(DirName)

时间:2020-02-15 15:21:01      阅读:69      评论:0      收藏:0      [点我收藏+]

目录操作相关基础~~~

(1)递归创建目录

(2)删除目录

(3)判断目录是否为空

(4)判断文件是否隐藏

(5)获取目录大小

(6)在指定目录中查找目录

(7)获取文件的上级目录

(8)获取目录最后修改时间

(9)打印目录结构

(10)遍历指定目录下的所有目录

(11)遍历指定目录下所有文件

(12)在指定目录中查找文件

(13)遍历系统根目录

(14)查看当前工作目录

(15)遍历目录

  1 import java.io.*;
  2 import java.util.*;
  3 
  4 public class DirNameControl {
  5     // 递归创建目录
  6     private static void CreatDirname() {
  7         String dirname = "E:\\A\\B\\C\\D\\E\\F";
  8         File file = new File(dirname);
  9         boolean result = file.mkdirs();
 10         System.out.println("Status:" + result);
 11     }
 12 
 13     // 删除目录
 14     private static boolean DeleteDir(File dir) {
 15         if (dir.isDirectory()) {
 16             String[] children = dir.list();
 17             for (int i = 0; i < children.length; i++) {
 18                 boolean success = DeleteDir(new File(dir, children[i]));
 19                 if (!success) {
 20                     return false;
 21                 }
 22             }
 23         }
 24         if (dir.delete()) {
 25             System.out.println("目录已删除");
 26             return true;
 27         } else {
 28             System.out.println("目录删除失败");
 29             return false;
 30         }
 31     }
 32 
 33     private static void DelDirname() throws Exception {
 34         try {
 35             // 删除当前目录下的 test 目录
 36             DeleteDir(new File("./test"));
 37         } catch (Exception e) {
 38             e.printStackTrace();
 39         }
 40     }
 41 
 42 
 43     // 判断目录是否为空
 44     private static void DirnameIfNull() {
 45         File file = new File("/test");
 46         if (file.isDirectory()) {
 47             String[] files = file.list();
 48             if (files.length > 0) {
 49                 System.out.println("目录" + file.getPath() + "不为空!");
 50             }
 51         }
 52     }
 53 
 54 
 55     // 判断文件是否隐藏
 56     private static void FileIfHide() {
 57         File file = new File("E:/DEMO.txt");
 58         System.out.println(file.isHidden());
 59     }
 60 
 61 
 62     // 获取目录大小
 63     private static void DirnameSize() {
 64         long size;
 65         File file = new File("E:/JAVA_CODE");
 66         size = file.length();
 67         System.out.println("Size: " + size + "bytes");
 68     }
 69 
 70 
 71     // 在指定目录中查找文件
 72     private static void SearchFileInDirname() throws Exception {
 73         File dir = new File("../java");
 74         String[] children = dir.list();
 75         if (children == null) {
 76             System.out.println("目录不存在");
 77         } else {
 78             for (int i = 0; i < children.length; i++) {
 79                 String filename = children[i];
 80                 System.out.println(filename);
 81             }
 82         }
 83     }
 84 
 85 
 86     // 获取文件的上级目录
 87     private static void GetFileFatherDirname() {
 88         File file = new File("E:/FILE/test");
 89         String fatherDirname = file.getParent();
 90         System.out.println("文件的上级目录为:" + fatherDirname);
 91     }
 92 
 93 
 94     // 获取目录最后修改时间
 95     private static void GetDirnameLastDate() {
 96         File file = new File("E:/FILE/text.txt");
 97         System.out.println("文件最后修改时间为:" + new Date(file.lastModified()));
 98     }
 99 
100 
101     // 打印目录结构
102     public static void ShowDir(int indent, File file) throws IOException {
103         for (int i = 0; i < indent; i++) {
104             System.out.println("-");
105         }
106         System.out.println(file.getName());
107         if (file.isDirectory()) {
108             File[] files = file.listFiles();
109             for (int i = 0; i < files.length; i++) {
110                 ShowDir(indent + 4, files[i]);
111             }
112         }
113     }
114 
115     private static void PrintDirnameStruct() throws IOException {
116         ShowDir(1, new File("D:\\JAVA"));
117     }
118 
119 
120     // 遍历指定目录下所有目录
121     private static void TraverseAllDirname() {
122         File dir = new File("F:");
123         File[] files = dir.listFiles();
124         FileFilter fileFilter = new FileFilter() {
125             public boolean accept(File file) {
126                 return file.isDirectory();
127             }
128         };
129         files = dir.listFiles(fileFilter);
130         System.out.println(files.length);
131         if (files.length == 0) {
132             System.out.println("目录不存在或他不是一个目录");
133         } else {
134             for (int i = 0; i < files.length; i++) {
135                 File filename = files[i];
136                 System.out.println(filename.toString());
137             }
138         }
139     }
140 
141 
142     // 遍历指定目录下所有文件
143     private static void TraverseAllFileOfDirname() {
144         File dir = new File("E:");
145         String[] children = dir.list();
146         if (children == null) {
147             System.out.println("目录不存在或他不是一个目录");
148         } else {
149             for (int i = 0; i < children.length; i++) {
150                 String filename = children[i];
151                 System.out.println(filename);
152             }
153         }
154     }
155 
156 
157     // 遍历系统根目录
158     private static void TraverseSystemRootDirname() {
159         File[] roots = File.listRoots();
160         System.out.println("系统所有根目录:");
161         for (int i = 0; i < roots.length; i++) {
162             System.out.println(roots[i].toString());
163         }
164     }
165 
166 
167     // 查看当前工作目录
168     private static void GetWorkDirname() {
169         String curDir = System.getProperty("user.dir");
170         System.out.println("当前工作目录:" + curDir);
171     }
172 
173 
174     // 遍历目录
175     public static void visitedAllDirname(File dir) {
176         System.out.println(dir);
177         if (dir.isDirectory()) {
178             String[] children = dir.list();
179             for (int i = 0; i < children.length; i++) {
180                 visitedAllDirname(new File(dir, children[i]));
181             }
182         }
183     }
184 
185     private static void TraverseDirname() throws Exception {
186         System.out.println("遍历目录:");
187         File dir = new File("E:/JAVA_CODE/Code/JavaEE");    // 要遍历的目录
188         visitedAllDirname(dir);
189     }
190 
191     public static void main(String[] args) throws Exception {
192         CreatDirname();
193         DelDirname();
194         DirnameIfNull();
195         FileIfHide();
196         DirnameSize();
197         SearchFileInDirname();
198         GetFileFatherDirname();
199         GetDirnameLastDate();
200         PrintDirnameStruct();
201         TraverseAllDirname();
202         TraverseAllFileOfDirname();
203         TraverseSystemRootDirname();
204         GetWorkDirname();
205         TraverseDirname();
206     }
207 }

 

目录操作(DirName)

原文:https://www.cnblogs.com/skygrass0531/p/12311443.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!