如果是统计单词的话,参考
http://blog.csdn.net/treeroot/archive/2004/12/29/232797.aspx

解决方案 »

  1.   

    import java.util.*;
    import java.io.*;class Counter{
    private int i = 1;
    int read(){
    return i;
    }
    void increment(){
    i++;
    }
    }public class WordCount{
    private FileReader f;
    private TreeMap counts = new TreeMap();
    private StreamTokenizer st;
    WordCount(String file) throws FileNotFoundException{
    f = new FileReader(file);
    st = new StreamTokenizer(
    new BufferedReader(f));
    }
    void countWords(){
    try{
    //st.ordinaryChar('e');
    //st.eolIsSignificant(true);
    while(st.nextToken() != StreamTokenizer.TT_EOF){
    String s;
    switch(st.ttype){
    case StreamTokenizer.
    TT_EOL: s = "EOL";
    break;

    case StreamTokenizer.TT_NUMBER: 
    s = String.valueOf(st.nval);
    break; 

    case StreamTokenizer.TT_WORD: 
    s = st.sval;break;

    default: 
    s = String.valueOf((char)st.ttype);
    }
    if (counts.containsKey(s))
    ((Counter)counts.get(s)).increment();
    else
    counts.put(s, new Counter());
    }
    }catch(IOException e){
    System.err.println("countWords() unsuccessful");
    }
    }
    Collection values(){
    return counts.values();
    }
    Set keySet(){
    return counts.keySet();
    }
    Counter getCounter(String key){
    return (Counter)counts.get(key);
    }
    void cleanup(){
    try{
    f.close();
    }catch(IOException e){
    System.err.println("f.close() unsuccessful");
    }
    }

    public static void main(String[] args) throws FileNotFoundException{
         WordCount wc = new WordCount("Test.txt");
         wc.countWords();
         Iterator it = wc.keySet().iterator();
         String k;
         while(it.hasNext()){
         k = (String)it.next();
         System.out.println(k + " : " +
         wc.getCounter(k).read());
         }
        }

    }
      

  2.   

    IT|06年3G手机走进生活|搜狐
    数码|市场热点:全面解读数码相机今年的四大焦点|搜狐
    IT|将建三张3G网?|搜狐
    数码|宽屏手机成新热点|搜狐
    数码|最新宽屏手机逐个数|搜狐
    数码|最新宽屏手机逐个数|新浪上面是我所说的TXT文件里的内容,我想把这里面有“最新宽屏手机逐个数”是重复的。把它重复的个数取出来。并把“数码|最新宽屏手机逐个数|搜狐
    ”放在最前面。谢谢大家的支持!感谢!
      

  3.   

    阿訇哈。。
    强盗。。
    我当小偷
    跟楼主一起学习
    import java.util.*;
    import java.io.*;class Counter{
    private int i = 1;
    int read(){
    return i;
    }
    void increment(){
    i++;
    }
    }public class WordCount{
    private FileReader f;
    private TreeMap counts = new TreeMap();
    private StreamTokenizer st;
    WordCount(String file) throws FileNotFoundException{
    f = new FileReader(file);
    st = new StreamTokenizer(
    new BufferedReader(f));
    }
    void countWords(){
    try{
    //st.ordinaryChar('e');
    //st.eolIsSignificant(true);
    while(st.nextToken() != StreamTokenizer.TT_EOF){
    String s;
    switch(st.ttype){
    case StreamTokenizer.
    TT_EOL: s = "EOL";
    break;

    case StreamTokenizer.TT_NUMBER: 
    s = String.valueOf(st.nval);
    break; 

    case StreamTokenizer.TT_WORD: 
    s = st.sval;break;

    default: 
    s = String.valueOf((char)st.ttype);
    }
    if (counts.containsKey(s))
    ((Counter)counts.get(s)).increment();
    else
    counts.put(s, new Counter());
    }
    }catch(IOException e){
    System.err.println("countWords() unsuccessful");
    }
    }
    Collection values(){
    return counts.values();
    }
    Set keySet(){
    return counts.keySet();
    }
    Counter getCounter(String key){
    return (Counter)counts.get(key);
    }
    void cleanup(){
    try{
    f.close();
    }catch(IOException e){
    System.err.println("f.close() unsuccessful");
    }
    }

    public static void main(String[] args) throws FileNotFoundException{
         WordCount wc = new WordCount("Test.txt");
         wc.countWords();
         Iterator it = wc.keySet().iterator();
         String k;
         while(it.hasNext()){
         k = (String)it.next();
         System.out.println(k + " : " +
         wc.getCounter(k).read());
         }
        }

    }