在Txt文件中搜索字符串

时间:2014-03-24 19:36:21

标签: java string

我们在课堂上编写了这段代码,但我无法理解/操纵它。我想修改它,以便它需要用户输入,而不是搜索整个文件,并打印出文件中单词的位置。

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Map.Entry;

public class Assingment7 {

    public Map<String, Integer> getWordCount(String fileName){

        FileInputStream fis = null;
        DataInputStream dis = null;
        BufferedReader br = null;
        Map<String, Integer> wordMap = new HashMap<String, Integer>();
        try {
            fis = new FileInputStream(fileName);
            dis = new DataInputStream(fis);
            br = new BufferedReader(new InputStreamReader(dis));
            String line = null;
            while((line = br.readLine()) != null){
                StringTokenizer st = new StringTokenizer(line, " ");
                while(st.hasMoreTokens()){
                    String tmp = st.nextToken().toLowerCase();
                    if(wordMap.containsKey(tmp)){
                        wordMap.put(tmp, wordMap.get(tmp)+1);
                    } else {
                        wordMap.put(tmp, 1);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try{if(br != null) br.close();}catch(Exception ex){}
        }
        return wordMap;
    }

    public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){

        Set<Entry<String, Integer>> set = wordMap.entrySet();
        List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
        Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
        {
            public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
            {
                return (o2.getValue()).compareTo( o1.getValue() );
            }
        } );
        return list;
    }

    public static void main(String a[]){
        Assingment7 mdc = new Assingment7();
        Map<String, Integer> wordMap = mdc.getWordCount("C://fox.txt");
        List<Entry<String, Integer>> list = mdc.sortByValue(wordMap);
        for(Map.Entry<String, Integer> entry:list){
            System.out.println(entry.getKey()+" ==== "+entry.getValue());
        }
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以将主要方法更改为此类 - 用户在控制台中输入要搜索的字符串,如果在地图中找到此键,则会输出与其关联的值。

public static void main(String a[]) {
    Assingment7 mdc = new Assingment7();
    Map<String, Integer> wordMap = mdc.getWordCount("C://fox.txt");
    List<Entry<String, Integer>> list = mdc.sortByValue(wordMap);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String input = null;

    try {
      input = br.readLine();
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }

    for(Map.Entry<String, Integer> entry:list) {
        if(entry.getKey().equalsIgnoreCase(input)){
            System.out.println(entry.getKey() + " ==== " + entry.getValue());
        }
    }
}