Collect TXT files and subfolders, and count the number of words in the TXT files.

Source: Internet
Author: User
Tags readfile

// Filereader. Java

Package com. Xie. Tencent;

Import java. Io. file;
Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. List;
Import java. util. Set;
Import java. util. RegEx. pattern;

/**
* This is the last question of the tencent2010-10-10's Java test.
* Read the *. txt file of a folder and Its subfolders, and output all file names.
* At the same time, you need to read the words that appear in the file and count the number of words that appear,
* Separated by "," in the file. It can also be another separator.
* @ Author Center
*
*/
/*
* The TXT file included in this kind of real-time analysis file, and provides the method output file name
*/
Public class filereader {
/**
* Find the required file type based on input parameters. algorithm: Recursion
* @ Author Center
* @ Param basedirname: directory of the file (either a directory or a file)
* @ Param targetfilename the type of the file to be searched (regular expression)
* @ Param resultfiles: List set of related files
*/
Public void findfiles (string basedirname, string targetfilename, list <File> resultfiles ){
String tmpfilename = NULL;
Pattern P = NULL;
File basedir = new file (basedirname );
If (! Basedir. exists ()){
System. Out. println ("file or folder" + basedirname + "does not exist. ");
Return;
}
If (basedir. isdirectory ()){
// If basedir is a folder, recursively analyze the folder
String [] filelist = basedir. List ();
For (INT I = 0; I <filelist. length; I ++ ){
// File readfile = new file (basedirname + "//" + filelist [I]); // note the difference between // and/
File readfile = new file (basedirname + "/" + filelist [I]);
If (readfile. isdirectory ()){
Findfiles (basedirname + "/" + filelist [I], targetfilename, resultfiles );
} Else {
Tmpfilename = readfile. getname ();
P = pattern. Compile (targetfilename );
If (P. matcher (tmpfilename). Matches ()){
Resultfiles. Add (readfile. getabsolutefile ());
}
}
}

} Else {
// If basedir is not a folder, match the file directly.
Tmpfilename = basedir. getname ();
P = pattern. Compile (targetfilename );
If (P. matcher (tmpfilename). Matches ()){
Resultfiles. Add (basedir. getabsolutefile ());
}
}
}
/**
* @ Author Center
* @ Param alist stores the set of files found
* @ Return a hashmap stores the words in the file and the number of words.
*/
Public hashmap <string, integer> count (list <File> alist ){
Counterword CW = new counterword (this );
For (INT I = 0; I <alist. Size (); I ++ ){
File F = alist. Get (I );
CW. openfile (F. getabsolutepath ());

// System. Out. println ("file" + (I + 1) + ":" + F. getabsolutepath ());
}
Return CW. gethmap ();
}
 
Public static void main (string [] ARGs ){
Filereader Fr = new filereader ();
List <File> alist = new arraylist <File> ();
String base = "D: // project // tencentlast // Files"; // words in the file are separated by commas (,).
String fileendrex = "// W. + TXT $)"; // (regular expression of the .txt File
Fr. findfiles (base, fileendrex, alist );
Hashmap <string, integer> hmap = Fr. Count (alist );
Set <string> hset = hmap. keyset ();
For (iterator <string> iterator = hset. iterator (); iterator. hasnext ();){
String word = (string) iterator. Next ();
System. Out. println ("word:" + word + "" + hmap. Get (Word ));
}
}
}
// Countword. Java

Package com. Xie. Tencent;

Import java. Io. bufferedreader;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. util. hashmap;

/**
* This class is used to count the number of times a word appears in all files.
* @ Author Center
*
*/
Public class counterword {
Filereader fr;
Private hashmap <string, integer> hmap = new hashmap <string, integer> ();;

Public hashmap <string, integer> gethmap (){
Return hmap;
}
/**
* This constructor is used to hold references from the other party.
* @ Author Center
* @ Param F filereader
*/
Public counterword (filereader f ){
Fr = F;
}
/**
* Open the file based on the input file path and analyze the number of words in the file,
* Store a hashmap
* @ Author Center
* @ Param path the absolute path of the string File
* @ Return the returned value seems useless and can be received as needed
*/
Public Boolean openfile (string path ){
Boolean B = true;
Try {
Fileinputstream FCM = new fileinputstream (PATH );
Bufferedreader BR = new bufferedreader (New inputstreamreader (FCM ));
Try {
String words = Br. Readline ();
While (words! = NULL ){
// System. Out. println (words );
String [] Word = words. Split (",");
For (INT I = 0; I <word. length; I ++ ){
Addwordcount (word [I]);
}
Words = Br. Readline ();
}
} Catch (ioexception e ){
System. Out. println ("An error occurred while reading the file. ");
E. printstacktrace ();
}

} Catch (filenotfoundexception e ){
System. Out. println ("the file is not found:" + path );
B = false;
E. printstacktrace ();
}
Return B;
}
/**
* Put words into map and count the number of times a word appears.
* @ Param word string word
*/
Private void addwordcount (string word ){
If (hmap. containskey (Word )){
Hmap. Put (word, hmap. Get (Word). intvalue () + 1 );
} Else {
Hmap. Put (word, 1 );
}
}
 

}

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.