Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of displaying directory file list and deleting directory by java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "java how to display directory file list and delete directory function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "java how to achieve the function of displaying directory file list and deleting directory"!

Taking the d:\ a directory as an example, assume that the structure in the D:\ a directory is as follows:

D:\ a |-- a.sql |-- back.log |-- b | | |-- 1.txt | |-- 2.txt | | `--3.txt |`-- f | |-- 4.txt | |-- 5.txt | `--6.txt |-- c | | |-- ace1.txt | | |-- ace2.txt | |`-- ace3.txt | `--4.txt |-- 5.txt |`-- 6.txt`-- d |-- | A.java |-- abc (1) .txt |-- abc (2) .txt |-- abc (3) .txt |-- b.java `--c.java

4.1 example 1: list the files in the entire directory (recursive)

Train of thought:

1. Traverse the directory d:\ a.

two。 Traverse this subdirectory every time you traverse to a directory in d:\ a. Therefore, you need to determine whether each traversed element is a directory.

The following is part of the code from normal code to recursive code:

File dir = new File ("dazzle a"); File [] file_list = dir.listFiles (); for (File list: file_list) {if (list.isDirectory ()) {File dir_1 = list.listFiles (); / / the code is repeated here, and the logic may be infinitely recursive if (dir_1.isDirectory ()) {.... }} else {System.out.println (list.getAbsolutePath ());}}

The repeated part of the code is encapsulated, so the recursive method is used to encapsulate the code and solve the infinite recursion problem. The final code is as follows:

Import java.io.*;public class ListAllFiles {public static void main (String [] args) {File dir = new File ("dazzled a"); System.out.println ("dir- >" + dir.getAbsolutePath ()); listAll (dir);} public static void listAll (File dir) {File [] file_list = dir.listFiles () For (File file: file_list) {if (file.isDirectory ()) {System.out.println ("dir- >" + file.getAbsolutePath ()); listAll (file);} else {System.out.println ("file- >" + file.getAbsolutePath ());}}

4.2 example 2: list the files (queues) in the entire directory

Train of thought:

1. Iterate through the given directory. Put the traversed directory name into the collection.

two。 Traverses each directory element in the collection, adds the traversed subdirectory to the collection, and finally removes it from the collection at the end of each traversal.

3. In this way, whenever a directory is found, it will continue to traverse until the entire directory has been traversed, and the next sibling directory will be traversed.

What needs to be considered is what kind of collection to use. First of all, the directory elements in the collection do not need to be sorted, and the names of subdirectories in different directories may be duplicated, so List collections are used instead of set collections, and because elements are added and deleted frequently, linkedlist is used instead of arraylist collections. The most prominent feature of linkedlist collections is FIFO queues.

Compared to recursive traversal, the advantage of using queues to traverse directories is that elements are placed in containers, they are all in heap memory, and memory overflows are not easy.

Import java.util.*;import java.io.*;public class ListAllFiles2 {public static void main (String [] args) {File dir = new File ("djjanxa"); Queue file_queue = new Queue (); / / build a queue File [] list = dir.listFiles (); for (File file: list) {/ / traverse the top-level directory if (file.isDirectory ()) {System.out.println ("dir- >" + file.getAbsolutePath ()) File_queue.add (file);} else {System.out.println ("file- >" + file.getAbsolutePath ());}} while (! file_queue.isNull ()) {/ / starting from the secondary subdirectory, traverse File subdirs = file_queue.get () layer by layer; / / first get the secondary subdirectory name File [] subFiles = subdirs.listFiles () For (File subdir: subFiles) {/ / traverse each next-level subdirectory if (subdir.isDirectory ()) {System.out.println ("dir- >" + subdir.getAbsolutePath ()); file_queue.add (subdir); / / if there are other subdirectories in the inner layer, add to the queue} else {System.out.println ("file- >" + subdir.getAbsolutePath ());}} class Queue {private LinkedList linkedlist Queue () {linkedlist = new LinkedList ();} public void add (E) {linkedlist.addFirst (e); / / Advanced} public E get () {return linkedlist.removeLast (); / / first out} public boolean isNull () {return linkedlist.isEmpty ();}}

4.3 example 3: the tree structure displays the files in the entire directory (recursive)

Train of thought:

1. First list the first-level directories and files.

two。 If it is a directory, add a prefix symbol that makes up the tree. Then iterate through this directory, where you need to iterate recursively.

Import java.io.*;public class TreeFiles {public static void main (String [] args) {File dir = new File ("djjizzag a"); System.out.println (dir.getName ()); listChilds (dir,1);} public static void listChilds (File fminint level) {String prefix = "; for (int item0tI)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report