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 does Java traverse folders

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains "how to traverse folders in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how Java traverses folders".

/ * * traverse the folder * /

Public class TraversalFolder {/ * depth-first traversal of folders *

* key points: use recursion to implement. * * [@ param] (https://my.oschina.net/u/2303379) topFile * [@ param] (https://my.oschina.net/u/2303379) orderedList * / public static void depthFirstSearch (File topFile, ArrayList orderedList) {if (null! = topFile) {if (topFile.isDirectory ()) {File [] files = topFile.listFiles () For (File file: files) {if (file.isDirectory ()) {depthFirstSearch (file, orderedList);} else {orderedList.add (file);} else {orderedList.add (topFile) }} / * breadth priority traversal of folders *

* main points: use the first-in-first-out feature of the stack to achieve. * * [@ param] (https://my.oschina.net/u/2303379) topFile * [@ param] (https://my.oschina.net/u/2303379) orderedList * [@ param] (https://my.oschina.net/u/2303379) maxNum * / public static void breadthFirstSearch (File topFile, ArrayList orderedList, int maxNum) {if (null! = topFile) {LinkedBlockingQueue queue = new LinkedBlockingQueue (maxNum); queue.offer (topFile) While (! queue.isEmpty ()) {File file = queue.poll (); if (file.isDirectory ()) {File [] files = file.listFiles (); for (File f: files) {if (f.isDirectory ()) {queue.offer (f) } else {orderedList.add (f);} else {orderedList.add (file) } public static void main (String [] args) {String path = "C:\\ Users\\ jxn\\ Desktop\\ New folder (3)"; File topFile = new File (path); ArrayList orderedList = new ArrayList (); / / depthFirstSearch (topFile,orderedList); breadthFirstSearch (topFile,orderedList, 100) OrderedList.stream () .forEach ((x)-> {System.out.println (x.getAbsoluteFile ());});}

}

At this point, I believe you have a deeper understanding of "how Java traverses folders". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Servers

Wechat

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

12
Report