不多说,不废话,直接上代码

package com.lyz.lucene.ana;

import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;

/**
* @author liuyazhuang
*
*/
public class AnalyzerTest {
/**
* 将分词后的结果输出
* @param analyzer
* @param txt
* @throws Exception
*/
public void testAnalyzer(Analyzer analyzer, String txt) throws Exception{
TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(txt));
tokenStream.addAttribute(TermAttribute.class);
while (tokenStream.incrementToken()) {
TermAttribute termAttribute = tokenStream.getAttribute(TermAttribute.class);
System.out.println(termAttribute.term());
}
}
}