Use Java to read the number of rows in a file in a folder

Source: Internet
Author: User

Use Java to count the number of rows for all files in a folder  

The manager suddenly confessed to a task: a requirement to count the number of rows for all files in a folder. One hours on the internet has not been resolved. Later in the heart uncomfortable decided to write a Java class used to count the number of files, so spent two hours to write the code (see my Java Foundation is still pretty rotten). Although there are many areas to be improved, there is still a commemorative significance.

The core of this Java class is the ReadLine () method of the BufferedReader class, the indirect statistic number of rows, and the recursive traversal of the file.

This class is written to complete the task. The results are not very rigorous, and many situations do not take into account: for example, how do you want to read a certain type of file? This needs the students to work hard.

Students who want to reuse only need to change the path in the main method.

1  PackageTest;2 3 4 ImportJava.io.*;5 ImportJava.util.HashMap;6 ImportJava.util.Iterator;7 ImportJava.util.Map;8 9 /**Ten * Created by Yang Huabin on 2016/12/19. One  */ A  Public classStastiscs { -     /* - Main Method the      */ -      Public Static voidMain (string[] args) { -         //Enter the path, enter the path within the parentheses.  -File f =NewFile ("D:/languagepractise/java/hadoop/src/main/java"); + Plus (f); -     } +  A     /* at Traverse Folder -      */ -      Public Static voidPlus (File f) { -  -file[] files = f.listfiles ();//get all files for an incoming path -Map map =NewHashmap<string,integer> ();//the number of rows used to store statistics in  -         //traversal of these files, the requirements are not statistics in CVS, so add judgment to  +          for(File a:files) { -             //If the file name is CVS, skip the             if(A.getname (). Equals ("CVS")) { *                 Continue; $}Else {Panax Notoginseng                 //If a is a file, go to the next level of directory, otherwise count the number of rows -                 if(A.isdirectory ()) { the Plus (a); +}Else{ AMap = linenumber (A.getabsolutepath (), map);//number of methods for counting rows the                 } +             } -         } $  $         //results of output statistics - GetResult (map); -  the     } - Wuyi     /* the Count the number of rows for the corresponding file -      */ Wu      Public StaticMap<string,integer>linenumber (String f,map Map) { -         //define a character stream to read a file AboutFileReader FileReader =NULL; $         Try { -FileReader =NewFileReader (f); -}Catch(IOException e) { - e.printstacktrace (); ASYSTEM.OUT.PRINTLN ("The path entered is incorrect"); +         } the  -BufferedReader bufferedreader=NewBufferedReader (FileReader);//upgrade from a byte stream to a character stream for easy read-by-line.  $         intindex = 0; the  the  the         Try { the              while(Bufferedreader.readline ()! =NULL){ -index++; in             } theMap.put (F,index);//put the results in a map the  About  the}Catch(IOException e) { the e.printstacktrace (); theSystem.out.println ("This file cannot be read! "); +}finally { -             if(FileReader! =NULL){ the                 Try {Bayi filereader.close (); the}Catch(IOException e) { the e.printstacktrace (); -                 } -             } the             returnmap; the         } the     } the  -     /* the stores the number of rows in a map, and then outputs the number of rows and the      */ the      Public Static voidGetResult (map map) {94         intsum = 0; the         //traversing the Map collection using iterator theiterator<map.entry<string,integer>> entries = the Map.entryset (). iterator ();98  About          while(Entries.hasnext ()) { -Map.entry<string,integer> Entry =Entries.next ();101The number of System.out.println (Entry.getkey () + "lines is:" +Entry.getvalue ());102Sum + =Entry.getvalue ();103         }104  theSYSTEM.OUT.PRINTLN ("Total number of rows is:" +sum);106 107     }108}

Use Java to read the number of rows in a file in a folder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.