有关关键字的写入文件和读取

/***
这里用到了一个工具类StringUtil.java,在博客中有StringUtil.java这个类的说明
*/
package com.huanglq.huanglq_test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.huanglq.util.DBUtil;
import com.huanglq.util.Log;
import com.huanglq.util.StringUtil;

public class KeywordDaoImpl{

//在这里做一次转义处理
public static String[] getEscapedKeyWords(String keyword,int count){
String[] keyWords=getKeywords(keyword, count);
for(int i=0;i<keyWords.length;i++){
System.out.println(keyWords[i]);

if(keyWords[i].contains("\\")){
keyWords[i]=StringUtil.replace(keyWords[i], "\\", "\\\\");
}
if(keyWords[i].contains("'")){
keyWords[i]=StringUtil.replace(keyWords[i], "'", "\\'");
}
}
return keyWords;
}
/***************************************************************************
* 要先执行setKeywords(String filePath)一次,才能执行getKeywords(String keyword,int
* count,String filePath);
* 返回包合关键字的集合,keyword是要搜索的关键字,count是显示的个数,filePath是keywords存放的文件路径
*/
public static String[] getKeywords(String keyword, int count) {
// 文件中读取内容,返回一个keyword的String[]
String[] keywords = KEY_WORDS;

StringBuffer br = new StringBuffer("");
// 要返回的关键字集合String[]
String[] keys = new String[count];
List keysList = new ArrayList();
List keysList2 = new ArrayList();
for (int i = 0; i < keywords.length; i++) {
if ((keywords[i].toLowerCase()).startsWith(keyword.toLowerCase())) {
keysList.add(keywords[i].trim());

if (keysList.size() >= count)
break;
} else if (
(!(keywords[i].toLowerCase()).startsWith(keyword.toLowerCase()))
&& (keywords[i].toLowerCase()).contains(keyword.toLowerCase())) {
keysList2.add(keywords[i].trim());


}
}
Iterator it = keysList.iterator();
while (it.hasNext()) {
br.append((String) it.next() + "\n");
}
// 如果keyword开头的不够count的数,就把包含的也写进来,加到count,
int countKeyWord = keysList.size() ;
if (countKeyWord < count) {
Iterator it2 = keysList2.iterator();
while (it2.hasNext()) {
br.append((String) it2.next() + "\n");
countKeyWord++;
if (countKeyWord > count) {
break;
}
}
}

String tempKeys = br.toString();
keys = tempKeys.split("\n");

return keys;
}

// 读取文件-------------从文件中读取内容,返回一个keyword的String[]
private static String[] KEY_WORDS = null;
static {
refeshKeyWord();
}

//刷新keyword的文件
public void refeshKeyWordToFile(){
refeshKeyWord();
}
//初始化/刷新KEY_WORDS
public static void refeshKeyWord() {
FileReader fr = null;
BufferedReader br = null;
StringBuffer keys = new StringBuffer("");
try {
// 文件路径,这里的文件路径可以写在全局变量或配置在xml或.properties文件中
fr = new FileReader("/usr/work4/keyword.txt");
br = new BufferedReader(fr);
String len = "";
int index=1;
while ((len = br.readLine()) != null) {
//这里这样做是为了方便后面用换行来分隔字符串
keys.append(len.trim() + "\n");
}
String temp = keys.toString();
KEY_WORDS = temp.split("\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static String[] getKeyWord() {
return KEY_WORDS;
}

//这里用到来了Configuration.java,和refeshKeyword.xml
//Configuration是读取xml的类
//txtCount,txt[]的值都是从xml文件读取回来的
private static int txtCount;

private static String txt[] =new String[10];
static{
txtCount = Integer.parseInt(Configuration.getConfigValue(
"refeshKeyword.xml", "refeshKeyword", "TxtCount"));
for (int i = 1; i <= txtCount; i++) {
txt[i - 1] = Configuration.getConfigValue("refeshKeyword.xml",
"refeshKeyword", "Txt" + i);
}
}

// 找关键字的语句
String KEYWORD_SQL = "select top 10000 keyword,count(1) as cnt from huanglq90_other..all_keyword "
+ "group by keyword order by cnt desc";

// 生成关键字文件(add by huanglinquan)
public void setKeywords() {
String[] keyWords = null;
String temp = null;
StringBuffer sb = new StringBuffer("");
List keyWordList = new ArrayList();

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

FileWriter fw = null;

try {
conn = DBUtil.gethuanglq90Conn();//链接JNDI
//conn = DBUtil.getConnection(url2, driver, username, password);
pstmt = conn.prepareStatement(KEYWORD_SQL);
rs = pstmt.executeQuery();
while (rs.next()) {
keyWordList.add(rs.getString("keyword"));
}
for (int k = 0; k < txtCount; k++) {
fw = new FileWriter(txt[k]);
keyWords = new String[keyWordList.size()];
for (int i = 0; i < keyWords.length; i++) {
keyWords[i] = (String) keyWordList.get(i);
sb.append(keyWords[i] + "\n");
}
temp = sb.toString();
fw.write(temp);
fw.flush();
fw.close();
sb = new StringBuffer("");
Log.debug(this, "getKeywords()", KEYWORD_SQL);
}
} catch (Exception e) {
Log.error(this, "getKeywords()", e.getMessage());
} finally {
DBUtil.clean(this, rs);
DBUtil.clean(this, pstmt);
DBUtil.clean(this, conn);
}
}

}
===========================================
Suggestion.java是KeywordDaoImp.的改进,但是没有把setKeywords()写进来,
不过它还有一个不断调用getSuggest(String key, int count)时,在每天的指定时间就会刷新静态初始化块

package com.huanglq.huanglq_test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Date;

import com.huanglq.util.Log;
import com.huanglq.util.StringUtil;

public class Suggestion {

private static String path = "/usr/work4/keyword.txt";

// 返回关键词的个数
private static final int DEFAULT_COUNT = 10;

// 重新读取关键词数据的时间,8即是早晨8点,默认为8
private static final int LOAD_HOUR = 8;

// 重新读取关键词数据的间隔时间,单位:微妙;默认值为1天
private static final int LOAD_INTERVAL = 24 * 60 * 60 * 1000;

// 包含所有关键词的列表
private static String[] words = null;

private static long loodTime = 0;

private static final Locale locale = Locale.SIMPLIFIED_CHINESE;

// yyyy-mm-dd
private static final DateFormat FORMAT_DATE = DateFormat.getDateInstance(
DateFormat.MEDIUM, locale);

// yyyy-mm-dd HH:SS:MM
private static final DateFormat FORMAT_DTTM = DateFormat
.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);

static {
load();
}

public String[] get(String key, int count) {
return getEscapedSuggest(key, count);
}

public String[] get(String key) {
return get(key, DEFAULT_COUNT);
}

public static String[] getEscapedSuggest(String key, int count) {
String[] suggest = getSuggest(key, count);
for (int i = 0; i < suggest.length; i++) {
if (suggest[i].contains("\\")) {
suggest[i] = StringUtil.replace(suggest[i], "\\", "\\\\");
}
if (suggest[i].contains("'")) {
suggest[i] = StringUtil.replace(suggest[i], "'", "\\'");
}
}
return suggest;
}

public static String[] getSuggest(String key, int count) {
if (System.currentTimeMillis() >= loodTime)
load();

String[] suggest = null;

List keysList = new ArrayList();
List keysList2 = new ArrayList();

for (int i = 0; i < words.length; i++) {
if ((words[i].toLowerCase()).startsWith(key.toLowerCase())) {
keysList.add(words[i].trim());
if (keysList.size() >= count)
break;
} else if (words[i].toLowerCase().indexOf(key.toLowerCase()) > 0) {
if (keysList2.size() >= count)
continue;
keysList2.add(words[i].trim());
}
}

int need = count - keysList.size();
for (int i = 0; i < need; i++) {
if (keysList2.size() == 0 || keysList2.size() < (i + 1))
break;
keysList.add(keysList2.get(i));
}

suggest = new String[keysList.size()];
for (int i = 0; i < keysList.size(); i++) {
suggest[i] = (String) keysList.get(i);
}

return suggest;
}

static public void load() {
try {
System.out.println("load()");
String time = FORMAT_DATE.format(new Date()) + " " + LOAD_HOUR + ":0:0";
loodTime = FORMAT_DTTM.parse(time).getTime() + LOAD_INTERVAL;
} catch (ParseException e) {
Log.error(Suggestion.class, "load{}", e.getMessage());
}

FileReader fr = null;
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
fr = new FileReader(path);
br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line.trim() + "\n");
}
words = sb.toString().split("\n");
} catch (Exception e) {
Log.error(Suggestion.class, "reload()", e.getMessage());
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
Log.error(Suggestion.class, "reload()", e.getMessage());
}
}
}

public static String getPath() {
return path;
}

public static void setPath(String path) {
Suggestion.path = path;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值