Traffic directory, implement the directory deletion, obtain the directory size and depth

zhaozj2021-02-16  56

Import java.io. *; import java.util. *;

Public class management {private int Dir_deep = 0; private int dir_size = 0; private int sourcePath_deep; // Input path relative to the depth of the root directory HT = new hashtable (); // Stores all subdirectories under the input path, And its depth keys = (string) DirPath, Values ​​= (Integer) Dirdeep

public synchronized int getSize (String dir_path) throws FileNotFoundException {File dir = new File (dir_path); if (dir.exists ()!) {throw new FileNotFoundException (); (! dir.isDirectory ())} if {dir_size = ( INT) Dir.Length ();} else {file [] fe = dir.listfiles (); for (int i = 0; i

public synchronized void deleteFiles (String dir_path) throws FileNotFoundException {File file = new File (dir_path); if {throw new FileNotFoundException () (file.exists ()!);} if (file.isDirectory ()) {File [] fe = file.listfiles (); for (int i = 0; i

Private void alldeep (file file) {file [] fe = file.listfiles (); for (int i = 0; i

public synchronized int getDeep (String dir_path) throws FileNotFoundException {File file = new File (dir_path); if {throw new FileNotFoundException () (file.exists ()!);} if (! file.isDirectory ()) {return -1 ; // Input path is not a directory, return -1} sourcePath_deep = count (file.tostring ()); alldeep (file); enumeration enum = ht.keys (); int max = 0; String Keys; Integer Values; While (Enum.hasMoreElements ()) {keys = (string) enum.nexTelement (); values ​​= (integer) Ht.get (keys); if (Values.intValue ()> max) Max = Values.intValue () Return Max;} private int count (string str) {string regex = "/"; if ("file.separator"). Equals ("//")) // WINDOWS and UNIX Path Sections Different regex = ""; return str.split (regex) .length;}} The above class implements the operation of the directory, including deletion, size, and depth.

When the directory depth is taken, I obtain the depth of all subdirectories relative to the root directory by split string, and then subtract the depth of the input directory relative to the root directory, it gives the depth of the input directory. I feel that this method is not effective, and which friend has a better way, I hope I will enlighten me.

转载请注明原文地址:https://www.9cbs.com/read-21858.html

New Post(0)