Pairs of objects: Shary Weibo address: http://www.cnblogs.com/yanrui666/p/5303941.html github:https://github.com/yanrui666/yanrui123/blob/ master/Job 4 Contribution ratio: 50%/50%
Based on the results of Job 3, read a small text file A_tale_of_two_cities.txt, statistics the frequency of the word in the file, and output the statistic results to the Result1.txt file in the current directory. (first draft completed this requirement)
- command-line format: prompt > Myapp.exe-f filename.txt > Result.txt (ps:c++ program, Java program output similar) filename.txt the file name that you downloaded earlier.
- Explain:
- Option-F indicates the following file name
- Output format provisions (refer to example in Job 3):
- First sort by frequency from high to low
- Words of the same frequency, sorted in dictionary order
- In addition, read a large text file Gone_with_the_wind.txt Experiment Contrast program execution efficiency, make the following improvements, compared to improve the time before and after the program execution.
Java text file invocation:
BufferedReader infile = new BufferedReader (New FileReader ("D:\\a_tale_of_two_cities.txt"));
string string;
String file = null;
while ((string = Infile.readline ()) = null) {file + = string;}
Java Output text file:
BufferedWriter bw = new BufferedWriter (New FileWriter ("Result1.txt"));
SOURCE program:
Import java.util.*;
Import java.io.*;
public class Wordsrate {public static void main (string[] args) throws Exception {
BufferedReader infile = new BufferedReader (New FileReader ("D:\\a_tale_of_two_cities.txt"));
string string;
String file = null;
while (string = Infile.readline ()) = null) {file + = string;} file = File.tolowercase ();
File = File.replaceall ("[^a-za-z]", "");
File = File.replaceall ("\\s+", "" ");
String words[];
Words = File.split ("\\s+");
map<string, integer> hashMap = new hashmap<string, integer> ();
for (int i = 0; i < words.length; i++) {String key = Words[i];
if (hashmap.get (key) = null)
{
int value = ((Integer) Hashmap.get (Key)). Intvalue ();
value++;
Hashmap.put (Key, new Integer (value)); } else {Hashmap.put (key, New Integer (1));}} map<string, object> treeMap = new treemap<string, object> (HASHMAP);
map<string, object> treeMap1 = new treemap<string, object> (HASHMAP);
BufferedWriter bw = new BufferedWriter (New FileWriter ("Result1.txt"));
Iterator iter = Treemap.entryset (). Iterator ();
String ss1[]=new string[treemap.size ()];;
int ss2[]=new int[treemap.size ()];
int i=0;
while (Iter.hasnext ()) {Map.entry Entry = (map.entry) iter.next ();
int val = (Integer) entry.getvalue ();
String key = (string) entry.getkey ();
Ss1[i]=key;
Ss2[i]=val;
i++;
}
int svalue=0;
String skey= "";
for (int j=0;j<ss2.length;j++)
{
for (int k=0;k<i;k++)
{
if (Ss2[j]>ss2[k])
{SVALUE=SS2[J];
SKEY=SS1[J];
SS2[J]=SS2[K];
SS1[J]=SS1[K];
Ss2[k]=svalue;
Ss1[k]=skey;
}
}
}
int[] strlengths = new Int[ss2.length];
for (int j=0;j<ss2.length; J + +) {Strlengths[j] = Ss1[j].length ();}
for (int j=0;j<ss2.length;j++) {if (strlengths[j]>=4) System.out.println (ss1[j]+ "=" +ss2[j]); Bw.write (ss1[j]+ " = "+ss2[j]); Bw.newline (); Bw.flush (); }
}
}
Results:
Experience: Two people to compare their own more relaxed, some problems themselves can not find the other side to solve, very convenient
Job 4.1