Before writing the application module, the unit test code, incredibly open brain hole incredibly created an N-level directory, to delete the test results directory later, only to find that the deletion, the hint directory too long cannot be deleted. On the Internet to find some methods, also found some grinder, did not achieve the desired results, I shortened a small application, used to delete their own brain hole opened the creation of cascading directory. This small application does not have code comment, also did not carry on the fault-tolerant processing, please everybody knows! hahaha haha ..... If you find errors, please also let me know, I will revise the revisions, thank you very much!
Gossip less, directly on the code!
1 /**2 * AARON.FFP Inc.3 * Copyright (c) 2004-2015 All rights Reserved.4 */5 PackageFFP;6 7 ImportJava.io.File;8 9 /**Ten * One * @authorAARON.FFP A * @versionV1.0.0:deldir FFP Deldir.java, December 31, 2015 morning 10:14:23 Exp $ - */ - Public classDeldir { the Private StaticString msg = ""; - - /** - * + * @authorAARON.FFP - * @versionV1.0.0:deldir FFP Deldir.java Main, December 31, 2015 morning 10:14:23 Exp $ + * A * @paramargs at */ - Public Static voidMain (string[] args) { -String RootDir = args[0]; -String Delrootdir = args[1]; - - in Try { -File root =NewFile (rootdir); to + if(Root.exists ()) { - if(Root.isfile ()) { the Root.delete (); * $ return;Panax Notoginseng } - the Delfile (rootdir); + A if("Delete". Equals (Delrootdir.tolowercase ())) { the Root.delete (); + } - } $}Catch(Exception e) { $ System.out.println (E.getmessage ()); - } - } the - Public Static voiddelfile (String rootdir) {WuyiString filename = ""; the -file[] Flist =NewFile (RootDir). Listfiles (); Wu - for(inti = 0; i < flist.length; i++) { Aboutfilename =Flist[i].getpath (); $ - if(Flist[i].getpath (). Equals (RootDir) && (NewFile (RootDir). Listfiles (). length < 1)) { - return; - } A + if(Flist[i].isfile ()) { the if(Flist[i].delete ()) { -msg = "\ n----------------->> successed to delete file \n{" + filename + "}"; $ System.out.println (msg); the } the } the the if(Flist[i].isdirectory ()) { - if(Flist[i].listfiles (). length > 0) { in Delfile (Flist[i].getpath ()); the}Else { the if(Flist[i].delete ()) { Aboutmsg = "\n\n================= >> successed to delete folder \n{" + filename + "}"; the System.out.println (msg); the } the } + } - } the BayiFlist =NewFile (RootDir). Listfiles (); the if(Flist.length > 0) { the Delfile (rootdir); - } - } the}
Package The program (for example, package as: Cleandirectory.jar), open the command line, and execute the command:
1 Java-jar cleandirectory.jar "{root directory to delete}" "Delete"23 second parameter: whether to delete the root directory
At this point, the Java learning -040-Cascade Delete directory files, directories successfully completed, I hope this article can give beginners javaweb you a reference.
Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^
Java Learning -040-files, directories, and directories in a cascade delete directory