/**
* 根据dbpath得到记录总条数
* @param dbpath
* @return
*/
public static int getTotalFromDBPath(String dbpath){
IndexReader indexReader = null;
int maxnum = 0;
try{
indexReader = IndexReader.open(dbpath);
maxnum = indexReader.maxDoc();
indexReader.close();
}catch(Exception ex){
ex.printStackTrace();
}
return maxnum;
}
/**
* 根据dbpath得到字段数组
* @param dbpath
* @return
*/
public static String[] getFieldsFromDBPath(String dbpath){
IndexReader indexReader = null;
String[] fields = null;
try{
indexReader = IndexReader.open(dbpath);
int max = indexReader.maxDoc();
if(max > 0){
Collection conn = null;
conn = indexReader.getFieldNames();
fields = new String[conn.size() - 1];
Iterator iter = conn.iterator();
int fieldsnum = 0;
for (Iterator iterator = conn.iterator(); iterator.hasNext();) {
String s = (String) iterator.next();
if(!s.equals("")){
fields[fieldsnum++] = s;
}
}
}
indexReader.close();
}catch(Exception ex){
ex.printStackTrace();
}
return fields;
}
/**
* 根据dbpath得到记录集
* @param dbpath
* @return
*/
public static List getRecordFromDBPath(String dbpath,int substringlength){
ArrayList list = null;
IndexReader indexReader = null;
Document document = null;
String[] fields = null;
int maxnum = 0;
try{
indexReader = IndexReader.open(dbpath);
maxnum = indexReader.maxDoc();
//当记录集存在时, 取出记录集
if(maxnum > 0){
list = new ArrayList();
fields = AllView.getFieldsFromDBPath(dbpath);
String temp = "";
for(int n=0;n < maxnum; n++){
document = indexReader.document(n);
String[] record = new String[fields.length];
for(int m = 0; m < fields.length; m++){
temp = document.get(fields[m]);
//当substringlength不为-1, 且超过规定长度substringlength,则截取前substringlength个字符
if((temp.length() > substringlength) && (substringlength != -1)){
temp = temp.substring(0,substringlength)+" ...";
}
//若为空,则设为空格
if(temp.length() < 1){
temp = " ";
}
//将字段内容存入数组
record[m] = temp;
}
//将数组加入到list, 一个数组对象就是一条记录
list.add(record);
}
}
indexReader.close();
}catch(Exception ex){
ex.printStackTrace();
}
return list;
}
/**
* 得到完整的记录集结果, 不截取字段内容
* @param dbpath
* @return
*/
public static List getRecordFromDBPath(String dbpath){
return AllView.getRecordFromDBPath(dbpath,-1);
}
Trackback: http://tb.donews.net/TrackBack.aspx?PostId=288829