Lucene学习

      最近开始学习Lucene,手上的教材是《搜索引擎开发权威经典》,很早的一本书,但是还不错,书中的代码自己都打算敲一遍。

文章中的代码都是教材中的代码。

      Lucene体验实例:编写两个辅助类,首先要将Lucene-core-2.9.2导入编译路径里。。

 FileText.java,用来获得指定路径的文本文件内容:

package tool; import java.io.*; public class FileText { public static String getText(File f) { StringBuffer sb = new StringBuffer(""); try { FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String s = br.readLine(); while (s != null) { sb.append(s); s = br.readLine(); } br.close(); } catch (Exception e) { sb.append(""); } return sb.toString(); } public static String getText(String s) { String t = ""; try { File f = new File(s); t = getText(f); } catch (Exception e) { t = ""; } return t; } public static void main(String[] args) { String s = FileText.getText("123.htm"); System.out.println(s); } }

 

FileList.java用来获得指定文件目录下的所有文件:

package tool; import java.io.File; import java.io.IOException; public class FileList { private static final String SEP="/"; private static StringBuffer sb=new StringBuffer(""); //返回数组 public static String[]getFiles(File f)throws IOException { if(f.isDirectory()) { File[] fs=f.listFiles(); for(int i=0;i<fs.length ;i++) { getFiles(fs[i]); } } else { sb.append(f.getPath()+SEP); } String s=sb.toString(); String[] list=s.split(SEP); return list; } //返回数组,重载 public static String[] getFiles(String t)throws IOException { File f=new File(t); if(f.isDirectory()) { File[] fs=f.listFiles(); for(int i=0;i<fs.length;i++) { getFiles(fs[i]); } } else { sb.append(f.getPath()+SEP); } String s=sb.toString(); String[] list=s.split(SEP); return list; } //主函数,测试 public static void main(String[] args) throws IOException { String s[]=FileList.getFiles("D:/workspace/Lucene"); for(int i=0;i<s.length;i++) { System.out.println(s[i]);} } }

 

 

     

你可能感兴趣的:(Lucene学习)