第1个回答 2009-01-07
楼上LUOJUNSONG20的代码140行,我的代码不到50行。你是不是应该多给我加点分呢?
import java.io.*;
import java.util.regex.*;
import java.util.*;
public class WordCount {
public static void main(String[] args) throws Exception {
count("d:\\test3.txt");
}
public static void count(String filePath) throws Exception {
Hashtable<String, Integer> map = new Hashtable<String, Integer>();
String text = file2String(new File(filePath));
String[] words = text.split("\\W");
for(String s :words) {
if(s.equals("")) {continue;}
s = s.toLowerCase();
Integer count = map.get(s);
if(count == null) {count = 0;}
map.put(s, count + 1);
}
List<Map.Entry<String, Integer>> entrys =
new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(entrys, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> e1,
Map.Entry<String, Integer> e2) {
return e2.getValue() - e1.getValue();
}
});
for(int i = 0; i < entrys.size() && i < 5; i++) {
System.out.println(entrys.get(i).getKey() + "的个数为"
+ entrys.get(i).getValue());
}
}
public static String file2String(File file) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(file));
StringBuilder builder = new StringBuilder();
String s;
while ((s = in.readLine()) != null) {
builder.append(s);
builder.append("\n");
}
in.close();
return builder.toString();
}
}
第2个回答 2009-01-06
//JDK must be 1.4 and later
//Save as 'Statistics.java' ,Compile and run.
import java.io.*;
import java.util.*;
public class Statistics {
private static ArrayList words= new ArrayList();;
public static void main(String[] args) {
int top = 5;
String filePath = javax.swing.JOptionPane
.showInputDialog("Please input full path of file:\nExample: 'C:\\words.txt'");
if (filePath != null)
start(filePath, top);
}
private static void start(final String filePath, final int top) {
new Thread() {
public void run() {
Thread t = process(filePath);
if(t==null)
return;
t.start();
while (t.isAlive()) {
sleep();
if(t.isAlive())
System.out.print('*');
}
// Print the final result:
Collections.sort(words);
int size = words.size();
size = size < top ? size : top;
System.out.println("\nFile: " + filePath);
System.out.println("==========Top:" + top + "==========");
for (int i = 0; i < size; i++) {
System.out.println(words.get(i));
}
}
private void sleep() {
try {
sleep(500);
} catch (Exception e) {
}
}
}.start();
}
public static Thread process(final String f) {
File file = new File(f);
if(!file.exists()||!file.canRead()){
System.out.println("File '"+f+"' is not exists or can not be read.");
return null;
}
return process(new File(f));
}
public static Thread process(final File f) {
Thread t = new Thread() {
public void run() {
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String tmp = null;
int count = 0;
while ((tmp = br.readLine()) != null) {
++count;
String[] sp = tmp.trim().split("\\W+");
for (int i=0; i<sp.length; i++) {
String word = sp[i];
if (word.isEmpty())
continue;
WordWrapper wrapper = new WordWrapper(word);
int index = words.indexOf(wrapper);
if (index > -1)
((WordWrapper)words.get(index)).plus();
else
words.add(wrapper);
}
if (count % 100 == 0)
try {
sleep(50);
} catch (Exception e) {
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
return t;
}
}
class WordWrapper implements Comparable {
private String word;
private int amount;
public WordWrapper(String w) {
word = w;
plus();
}
public WordWrapper plus() {
++amount;
return this;
}
public int getAmount() {
return amount;
}
public String getWorld() {
return word;
}
public boolean equals(Object o) {
return word.equalsIgnoreCase(((WordWrapper) o).word);
}
public int compareTo(Object o) {
return ((WordWrapper)o).amount - amount;
}
public String toString() {
return word + " = " + amount;
}
}