Holen@Blog

Holen Bolg on Donews.net

  DonewsBlog  |  Donews首页  |  Donews社区  |  Donews邮箱  |  我的首页  |  联系作者  |  聚合   |  登录
  216篇文章 :: 0篇收藏:: 353篇评论:: 6个Trackbacks

公告

Free Counters

文章

收藏

相册

我的原创文章

存档


正在读取评论……


    /**

     * 根据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 = "&nbsp;";

                      }

                      //将字段内容存入数组

                     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


[点击此处收藏本文]  发表于2005年02月26日 12:58 AM




正在读取评论……

发表评论

大名:
网址:
验证码
评论