统计单词个数小工具

来源:互联网 发布:java敏捷开发平台 编辑:程序博客网 时间:2024/04/28 07:00

统计单词个数小工具

package learn;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.InputStreamReader;import java.util.Map;import java.util.StringTokenizer;import java.util.TreeMap;public class WorldStatistical {    public static void main(String[] argvs) {        WorldStatistical ws = new WorldStatistical();        ws.dataParser();    }    void  dataParser() {        Map<String, Integer> worldsMap = new TreeMap<String, Integer>();        String encoding = "UTF-8";        File dir = new File("C:\\Users\\acer\\Desktop\\许海华\\A Song of Ice and Fire(1-5)");        File[] fs = dir.listFiles();        for(int i=0; i<fs.length; i++){            try{                String filePath = fs[i].getAbsolutePath();                File file=new File(filePath);                if(file.isFile() && file.exists())                {                    InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);                    BufferedReader bufferedReader = new BufferedReader(read);                    String lineTxt = null;                    while((lineTxt = bufferedReader.readLine()) != null) {                        StringTokenizertokens = new StringTokenizer(lineTxt, " \t");                        int worldCount = tokens.countTokens();                        for(int j =0; j < worldCount; j++) {                            String dirtyWorld = tokens.nextToken().toLowerCase().trim();                            String curWorld = cleanWorld(dirtyWorld);                            if(curWorld == null) continue;                            Integer curCount = worldsMap.get(curWorld);                            if(curCount == null) {                                worldsMap.put(curWorld, 1);                            } else {                                worldsMap.put(curWorld, curCount+1);                            }                        }                    }                    bufferedReader.close();                }            } catch (Exception e) {                System.out.print("exception :" );                e.printStackTrace();            }        }        System.out.println(worldsMap.size());        writeWorldMap2File("C:\\Users\\acer\\Desktop\\许海华\\A Song of Ice and Fire(1-5)\\dict.txt", worldsMap);    }    String cleanWorld(String dirtyWorld) {        if (dirtyWorld.substring(0, 1).compareTo("a") < 0|| dirtyWorld.substring(0, 1).compareTo("z") > 0) {            if(dirtyWorld.length() < 3) return null;             return cleanWorld(dirtyWorld.substring(1, dirtyWorld.length()));        } else {            int indexWorldEnd = 0;            while(indexWorldEnd < dirtyWorld.length() && dirtyWorld.substring(indexWorldEnd, indexWorldEnd+1).compareTo("a") >= 0 && dirtyWorld.substring(indexWorldEnd, indexWorldEnd+1).compareTo("z") <= 0) {                indexWorldEnd++;            }            if(indexWorldEnd >= 4 && indexWorldEnd < 20) {                return dirtyWorld.substring(0, indexWorldEnd);            } else {                return null;            }        }    }    void writeWorldMap2File(String fileName, Map<String, Integer> worldsMap){        File destFile = new File(fileName);        if (!destFile.exists()) {            try {                destFile.createNewFile();            } catch (Exception e) {                e.printStackTrace();            }        }        try {            FileWriter fw = new FileWriter(destFile.getAbsoluteFile(), true);            BufferedWriter bw = new BufferedWriter(fw);            for(String world : worldsMap.keySet()) {                String content =  world + "\t" + worldsMap.get(world);                 bw.write(content);                bw.newLine();            }            bw.close();        }catch (Exception e) {            e.printStackTrace();        }    }}


0 0
原创粉丝点击