Java uses recursive algorithm to delete folders
Premise:
Integrated development environment (IDE): Eclipse
JDK version: 8.0
Several methods of file class:
1)isFile()
Test whether the file represented by this abstract pathname is a normal file.
2)list()
Returns an array of strings naming the files and directories in the directory represented by this abstract pathname.
3)delete()
Delete the file or directory represented by this abstract pathname.
4)listFiles()
Returns an array of abstract pathnames representing the files in the directory represented by the abstract pathname.
An attribute of the file class:
separator
The default name separator character associated with the system, expressed as a string in a convenient way.
code:
import java.io.File; public class diGui_delete { public static void deleteAll(File file) { if(file.isFile()||file.list().length==0) { file.delete(); }else { File files[] = file.listFiles(); for(File f :files) { deleteAll(f); f.delete(); } } } public static void main(String[] args) { File f1 = new File("d:"+File.separator+"漫画图片"); deleteAll(f1); } }
summary
The above is what Xiaobian introduced to you. Java uses recursive algorithm to delete folders. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!