打标签

来源:互联网 发布:自学编程先学什么 编辑:程序博客网 时间:2024/06/11 22:07
import java.io.*;import java.util.HashMap;import java.util.Map;import java.util.Properties;public class SQLCacheFile {    //stone库导出的地点X智能数据, 实例:江苏省南京市卫生局:0    public static HashMap<String,int[]> SQLCacheMap;    public  void loadSQLFile (String path) {        String encoding = "utf-8";        File file = new File(path);        try {            long now = System.currentTimeMillis();            if(file.isFile() && file.exists()) {                InputStreamReader read = new InputStreamReader(                        new FileInputStream(file),encoding                );                BufferedReader bufferedReader = new BufferedReader(read);                String lineText = "";                while((lineText=bufferedReader.readLine()) != null) {                    String[] parts = lineText.split(":");                    SQLCacheMap.put(parts[0],new int[]{Integer.parseInt(parts[1]),0});                }                System.out.println("读取文件完毕,耗费:"+(System.currentTimeMillis()-now)+"ms!");            } else {                System.out.println("读取文件失败!");            }        } catch (FileNotFoundException e) {            System.out.println(e);        } catch (UnsupportedEncodingException e) {            System.out.println(e);        } catch (IOException e) {            System.out.println(e);        } catch (Exception e) {            System.out.println(e);        }    }    /*        从kafka上接入消息        要接入分词功能,对传入的文章进行切词处理     */    public void getTitleAndTextWordCut(String title,String text) {        while(true) {            //text为从kafka上取得的文章正文和标题//            String[] titleWord = getWordCut(title);//            String[] textWord = getWordCut(text);//            for(int i =0; i<titleWord.length; i++) {//                checkIfExistsInSQLCache(titleWord[i]);//            }//            for(int i =0; i<textWord.length; i++) {//                checkIfExistsInSQLCache(textWord[i]);//            }        }    }    public void checkIfExistsInSQLCache(String word) {        if(SQLCacheMap.containsKey(word)) {            //如果词在词典中存在的话,则赋值为1            SQLCacheMap.get(word)[1] = 1;        }    }    public void writeSQLCacheIntoFile(String path) {        try {            FileOutputStream fos = new FileOutputStream(path);            OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");            for(Map.Entry<String,int[]> entry : SQLCacheMap.entrySet()) {                String govName = "";                //判断文件出现位是否为1,1代表出现过,0代表没有出现过                if(entry.getValue()[1] == 1) {                    //判断政府前缀为是否为1,1代表有前缀中共                    if(entry.getValue()[0] == 1) {                        govName = "中共"+ entry.getKey();                    }else {                        govName = entry.getKey();                    }                }                osw.write(govName+System.getProperty("line.separator"));            }            osw.close();        } catch (FileNotFoundException e) {            System.out.println(e);        } catch (UnsupportedEncodingException e) {            System.out.println(e);        } catch (IOException e) {            System.out.println(e);        }    }    public static void main(String[] args) {        try {            Properties properties = new Properties();            FileInputStream  fis = new FileInputStream("config.properties");            properties.load(fis);            String SQLCacheFilePath = properties.getProperty("SQLCacheFilePath");            String FileSavePath = properties.getProperty("FileSavePath");            SQLCacheFile sqlCacheFile = new SQLCacheFile();            sqlCacheFile.loadSQLFile(SQLCacheFilePath);            fis.close();        } catch (FileNotFoundException e) {            System.out.println(e);        } catch (IOException e) {            System.out.println(e);        }    }

}

SQLCacheFilePath=/User/cross/PractiseProject/sql.txtFileSavePath=/User/cross/PractiseProject/save.txtFileSaveInterval=1000

原创粉丝点击