Java file operations before N English words --- a single output file often occur

  As stated, reading a file, the first N output English words in a single file, the specific requirements are recordable 'words' from the figures, letters, alphanumeric symbols non delimiters. code show as below

 1 import java.io.BufferedReader;
 2 import java.io.File;
 3 import java.io.FileReader;
 4 import java.util.Map;
 5 import java.util.Map.Entry;
 6 import java.util.Scanner;
 7 import java.util.TreeMap;
 8 
 9 public class Traverse3 {
10     public static void main(String[] args) {
11         //读取文件
12         File file=new File("E:\\Harry Potter and the Sorcerer's Stone.txt");
13 is          Scanner in = new new Scanner (the System.in);
 14          the try {
 15              the FileReader fr = new new the FileReader (File);
 16              the BufferedReader BUFR = new new the BufferedReader (fr);
 . 17              // read the string returned by line 
18 is              String S = null ;
 . 19              // all s string concatenation 
20 is              string All = null ;
 21 is              the while (! (bufr.readLine s = ()) = null ) {
 22 is                  All All + = s;
 23 is              }
24              // definition of the TreeMap 
25              the Map <String, Integer> (TM) = new new the TreeMap <> ();
 26 is              // by looking for non-alphanumeric code division string, consisting of a string array 
27              String [] = all.split STRs ( "[ a-zA-Z0-9 ^] " );
 28              // array for storing strings 
29              string [] R1 = new new string [strs.length];
 30              // for storing the number string that appears array 
31 is              int [] = R2 new new  int [strs.length];
 32              // read the string array divided, using TreeMap stored string (key) and the number of occurrences (value) 
33 is              for ( int I = 0; I < strs.length; i ++) {
 34 is                  STRs [I] .toLowerCase ();
 35                  IF (! Tm.containsKey (STRs [I])) {
 36                      tm.put (STRs [I],. 1 );
 37 [                  } the else {
 38 is                      Integer = Counts (TM). GET (STRs [I]);
 39                      tm.put (STRs [I], + Counts. 1 );
 40                  }
 41 is              }
 42 is              int J = 0 ;
 43 is              // traverse TreeMap, and removal of key value, are stored in advance Construction good array 
44 is              for (the entry <String, Integer> entry: tm.entrySet ()) {
 45                 r1[j]=entry.getKey();
46                 r2[j]=entry.getValue();
47                 j++;
48             }
49             //冒泡降序排序
50             for(int p=0;p<strs.length-1;p++) {
51                 for(int q=0;q<strs.length-1-p;q++) {
52                     if(r2[q]<r2[q+1]) {
53                         int temp=r2[q];
54                         r2[q]=r2[q+1];
55                         r2[q+1]=temp;
56                         TEMS = String R1 [Q];
 57 is                          R1 [Q] = R1 [Q +. 1 ];
 58                          R1 [Q +. 1] = TEMS;
 59                      }
 60                  }
 61 is              }
 62 is              int L = in.nextInt ();
 63 is              // the first element of the array after completion of the storage of non-string (: r2 [0]), failed to eliminate the second output beginning. 
64              for ( int I =. 1; I <L +. 1; I ++ ) {
 65                  System.out.println (R1 [I] + ":" + R2 [I]);
 66              }
 67              // Close 
68             in.close();
69             bufr.close();
70             fr.close();
71         }catch(Exception e){
72             e.printStackTrace();
73         }
74     }
75 
76 }

  The task for the first time tried to use a TreeMap, initially intended to complete the direct deposit by TreeMap string, the number of statistics and sort of work, but there was value at the time of ordering the sorted values ​​are all 1 issue, and at the same time compared to the results of the completion of the code, key value "Sort" nor on the right, was seen reading the specified file only 453KB, TreeMap directly traverse the key value and the value stored in the two sub-arrays, and then bubble sort output, can be considered a tricky. Further, when TreeMap occurs by gripping a string of 'empty' string of the readLine () method returns a document is read by the line time without a carriage return and linefeed, the 'empty' string I do not know how to eliminate, so start output from the second element in the output.

  The mission exposed the main problem is to master the TreeMap, you need more practice.

Guess you like

Origin www.cnblogs.com/20183711PYD/p/11802010.html
Recommended