创建索引小程序 ,Lucene2.1

//按着课本写一个 Indext小程序   不过有点小错误 会出创建失败 不知道为啥??
//可能版本过时了那他练练手吧
package FirstSearcher;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

import tool.*;
//import java.io.FileReader;

public class LucenceIndexer {
        
        // TODO Auto-generated method stub

        /**
         * @param args
         */
        private JTextField jtfa;
        private JTextField jtfb;
        private JButton jba;
        private JButton jbb;
        private JButton jbc;
        private static JTextArea jta;
//      creatAndShowGUI用来设置索引器运行时的外观
        
        private void creatAndShowGUI()
        {
                //设置跨平台外观感受
        /*
         *平常做法 
         * String lf=UIManager.getCrossPlatformLookAndFeelClassName();
                try
                {
                        UIManager.setLookAndFeel(lf);
                }
                catch(Exception ce)
                {
                        JOptionPane.showMessageDialog(null,"无法设定外观感受!");
                }*/
                
                //java感受
                JFrame.setDefaultLookAndFeelDecorated(true);
                JFrame frame=new JFrame("Lixingle Indexer!1534432371@qq.COM");  
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                final JFileChooser fc=new JFileChooser();
                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                Container con=frame.getContentPane();
                con.setLayout(new BorderLayout());
                
                JPanel jpup=new JPanel();
                jpup.setLayout(new GridLayout(3,2));
                jtfa=new JTextField(30);
                jba=new JButton("选择被索引的文件存放路径");
                jba.addActionListener(
                                new ActionListener()
                                {
                                        public void actionPerformed(ActionEvent e)
                                        {
                                                int r=fc.showOpenDialog(null);
                                                if (r==JFileChooser.APPROVE_OPTION)
                                                {
                                                        jtfa.setText(fc.getSelectedFile().getPath());
                                                        jbc.setEnabled(true);
                                                }
                                                
                                        }
                                
                                }
                        
                                );
                jtfb=new JTextField(30);
                JButton jbb=new JButton("选择索引的存放路径");
                jbb.addActionListener(
                                new ActionListener()
                                {
                                        public void actionPerformed(ActionEvent e)
                                        {
                                                int r=fc.showOpenDialog(null);
                                                if (r==JFileChooser.APPROVE_OPTION)
                                                {
                                                        jtfb.setText(fc.getSelectedFile().getPath());
                                                        jbc.setEnabled(true);
                                                }
                                                
                                        }
                                        
                                }
                        
                                );
                JLabel jl=new JLabel("");
                jbc=new JButton("建立索引");
                
                jbc.addActionListener(
                                new ActionListener()
                                {
                                        public void actionPerformed(ActionEvent e)
                                        {
                                                try
                                                {
                                                        LuceneIndexderTool.index(jtfa.getText(),jtfb.getText());
                                                }
                                                catch (Exception ee)
                                                {
                                                        ee.printStackTrace();
                                                        jbc.setEnabled(true);
                                                        JOptionPane.showMessageDialog(null,"索引创建失败!");
                                                        System.out.println(ee.getMessage());
                                                        
                                                        
                                                }
                                        }
                                        
                                }
                        
                                );
                jpup.add(jtfa);
                jpup.add(jba);
                jpup.add(jtfb);
                jpup.add(jbb);
                jpup.add(jl);
                jpup.add(jbc);
                
                jta=new JTextArea(10,60);
                JScrollPane jsp=new JScrollPane(jta);
                con.add(jpup,BorderLayout.NORTH);
                con.add(jsp,BorderLayout.CENTER);
                frame.setSize(200,100);
                frame.pack();
                frame.setVisible(true);
        }
        
        //主函数用来总体控制
        
        public static void main(String[] args) {
                
                SwingUtilities.invokeLater(
                                new Runnable()
                                {
                                        public void run()
                                        {
                                                try
                                                {
                                                        new LucenceIndexer().creatAndShowGUI();
                                                }
                                                catch (Exception e)
                                                {
                                                        JOptionPane.showMessageDialog(null, "程序加载失败!");
                                                }
                                        }
                                }
                                
                                );
                
                
 }
        //使用内部类LuceneIndexderTool来实现索引工作,
        //这样,就可以把索引建立的情况反映在文本框里面了
        
        static class LuceneIndexderTool
        {
                //创建索引--被索引文件的路径,索引的路径
                public static void index(String filePath,String indexPath) throws IOException
                {
                        //建立索引器
                        IndexWriter writer=new  IndexWriter(indexPath,new StandardAnalyzer());
                        //递归遍历文件目录 来创建索引
                        String s[]=FileList.getFiles(filePath);
                        for  (int i=0;i<s.length;i++)
                        {
                                File f=new File(s[i]);
                                String ext=getExt(f);
                                if (ext.equalsIgnoreCase("htm")||ext.equalsIgnoreCase("html"))
                                                
                                {
                                        Document doc=new Document();
                                        //filename 字段
                                        String filename=f.getName();
                                        Field field=new Field("filename",filename,Field.Store.YES,Field.Index.TOKENIZED);
                                        doc.add(field);
                                        //uri字段
                                        String uri=f.getName();
                                        field=new Field("uri",uri,Field.Store.YES,Field.Index.NO);
                                        doc.add(field);
                                        //cdate字段
                                        Date dt=new Date(f.lastModified());
                                        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd E");
                                        String cdate=sdf.format(dt);
                                        field=new Field(" cdate", cdate,Field.Store.YES,Field.Index.NO);
                                        doc.add(field);
                                        //size 字段
                                        double si=f.length();
                                        String size="";
                                        if (si>1024)
                                        {
                                                size=String.valueOf(Math.floor(si/1024))+"k";
                                                
                                        }
                                        else 
                                        {
                                                size=String.valueOf(si)+"Bytes";
                                        }
                                        field=new Field("size",size,Field.Store.YES,Field.Index.NO);
                                        doc.add(field);
                                        //text字段
                                        String text=FileText.getText(f);
                                        field=new Field(" text", text,Field.Store.COMPRESS,Field.Index.TOKENIZED);
                                        doc.add( field);
                                        //digest字段
                                        String digest="";
                                        if (text.length()>200)
                                        {
                                                digest=text.substring(0,200);
                                        }
                                        else 
                                        {
                                                digest=text;
                                        }
                                        field=new Field("digest",digest,Field.Store.YES,Field.Index.TOKENIZED);
                                        doc.add( field);
                                        writer.addDocument(doc);
                                        
                                        jta.setText(jta.getText()+"已经导入索引: "+f+"\n");
                                }
                         }      
                        
                        writer.close();
                        JOptionPane.showMessageDialog(null, "索引建立完毕","乐乐提示",JOptionPane.INFORMATION_MESSAGE);
                        
                 
                }

                private static String getExt(File f) {
                        // TODO Auto-generated method stub
                        String s=f.getName();
                        try 
                        {
                                s=s.substring(s.lastIndexOf(".")+1);
                        }
                        catch(Exception e)
                        {
                                s="";
                        }
                        
                        
                        
                        return s;
                }
                
        }
        
        
}