Java Small Section -File Object Recursive Delete Directory All Contents

zhaozj2021-02-16  112

A file object, if it is a directory, then the delete () method requires a directory that is empty, otherwise it cannot be deleted. In order not to delete all the content under the specified folder, there is a small piece of small segment today, it is a small practice of beginner Java:

Import java.io. *; public class deleteall {// Remove all content under the folder, including this folder public void deLall (file f) throws ioException {if (! f.exists ()) // folder does not exist There is a THROW New IOException ("Specify directory does not exist:" f.GetName ()); boolean rslt = true; // Save intermediate result if (! (Rslt = f.delete ())) {// Try Direct Delete // If the folder is not empty. Enumeration, recursive deletion inside content file subs [] = f.ListFiles (); for (int i = 0; i <= subs.length - 1; i ) {if (SUBS [i] .ioDirectory ()) deLall ( SUBS [I]); // Remissive Delete subfolder content RSLT = SUBS [i] .delete (); // Delete subfolder itself}}}}}}}}}}} 本} (! rslt) throw new oException ("Unable to delete:" f.GetName ()); return;} public static void main (String [] args) {deleteall da = new deleteall (); try {Da.deLall (New File ("somedir"));} catch (ioException ex) {ex.printstacktrace ();}}}

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

New Post(0)