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 Java recursively traverses the file directory

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how Java recursively traverses the file directory". In the daily operation, I believe that many people have doubts about how Java recursively traverses the file directory. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how Java recursively traverses the file directory". Next, please follow the editor to study!

The code is as follows

Import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.Map;import java.util.Map.Entry;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.List;public class Traverse4 {public static void main (String [] args) {Traverse4 K = new Traverse4 (); / / call the traversal method K.Run ("E:\\ article") } / / Recursive traversal file public void Run (String AllFile) {File tfile = new File (AllFile); / / build file array File [] files = tfile.listFiles (); for (int I = 0; I < files.length) ) {/ / determine whether the file is a directory if (Files [I] .isDirectory ()) {/ / Yes, recursively call the function Run (Files [I] .getAbsolutePath ()) } else {/ / No, read the number of words output from the file System.out.println ("*" + Files [I] .getAbsolutePath () + "*"); ReadFile (Files [I]. GetAbsolutePath ());} / / count the number of words, sort, output public void ReadFile (String fname) {File file=new File (fname) Try {FileReader fr = new FileReader (file); BufferedReader bufr = new BufferedReader (fr); String s = null; / / create a hash table Map hm = new HashMap (); while ((s=bufr.readLine ())! = null) {/ / use regular expressions to segment the word String [] strs = s.split ("[^ a-zA-Z0-9]") / / put words into the hash table and count for (int I = 0; I < strs.length; iSuppli +) {strs[ I] .toLowerCase (); if (! hm.containsKey (strs [I])) {hm.put (strs [I], 1);} else {Integer counts = hm.get (strs[ I]) Hm.put (strs [I], counts+1);} / / call sorting method sort (hm); bufr.close (); fr.close ();} catch (Exception e) {e.printStackTrace ();}} public void sort (Mapmap) {/ / override List descending sort List infoIds = new ArrayList (map.entrySet ()) Collections.sort (infoIds, new Comparator () {public int compare (Map.Entry o1, Map.Entry O2) {return (o2.getValue ()-o1.getValue ();}}); / / output for (int I = 0; I < infoIds.size (); iTunes +) {Entry id = infoIds.get (I); System.out.println (id.getKey () + ":" + id.getValue ());}

This task only needs to write a method that can recursively traverse the file directory, the focus is still counting the number of words and sorting output, this time the statistics and sorting are all completed by HashMap, the statistical method is to use containsKey () to grab the existing key information before compiling the word, if the word has its value + 1, otherwise the new construction key will be stored in the form of a value of 1.

Sorting is to override the implementation of the List method. When you output, you should pay attention to traversing the rewritten List object. If you traverse the previously created HashMap object, the result is the data before sorting.

At this point, the study of "how to recursively traverse the file directory by Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report