在写索引前,先了解一下replace吧
replace
public String replace(char oldChar,char newChar)
-
Returns a new string resulting from replacing all occurrences of
oldChar
in this string withnewChar
. If the characteroldChar
does not occur in the character sequence represented by thisString
object, then a reference to thisString
object is returned. Otherwise, a newString
object is createdthat represents a character sequence identical to the character sequence represented by thisString
object, except that every occurrence ofoldChar
is replaced by an occurrence ofnewChar
. -
Examples:
"mesquite in your cellar".replace('e', 'o')
returns "mosquito in your collar""the war of baronets".replace('r', 'y')returns "the way of bayonets""sparring with a purple porpoise".replace('p', 't')returns "starring with a turtle tortoise""JonL".replace('q', 'x') returns "JonL" (no change)
-
-
-
Parameters:
-
oldChar
- the old character. -
newChar
- the new character.
Returns:
-
a string derived from this string by replacing every occurrence of
oldChar
withnewChar
.
-
前提代码
package
ch2.lucenedemo.process;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import jeasy.analysis.MMAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import jeasy.analysis.MMAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
这个程序迫切需要分词包和API包
public
class
IndexProcesser
{
// 成员变量,存储创建的文件存放位置
private String INDEX_STORE_PATH = " d:/index " ;
public void createIndex(String inputDir)
{
try
{
// 以MMAnalyzer作为分词工具创建一个IndexWriter
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH,
new MMAnalyzer(), true );
File filesDir = new File(inputDir);
// 取得所有需要建立索引的文件组
File[] files = filesDir.listFiles();
int length = files.length;
for ( int i = 0 ;i < length;i ++ )
{
String fileName = files[i].getName();
// 判断文件名是否为.txt文件
if (fileName.substring(fileName.lastIndexOf( " . " )).equals( " .txt " ))
{
Document doc = new Document();
// Field以后在学,Field.Index.TOKENIZED表示先分词后索引
Field field = new Field( " filename " ,files[i].getName(),Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
field = new Field( " content " ,loadFileToString(files[i]),Field.Store.NO,Field.Index.TOKENIZED);
doc.add(field);
writer.addDocument(doc);
}
}
writer.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String loadFileToString(File file)
{
try {
BufferedReader br = new BufferedReader( new FileReader(file));
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while (line != null )
{
sb.append(line);
line = br.readLine();
}
br.close();
return sb.toString();
}
catch (IOException e){
e.printStackTrace();
return null ;
}
}
public static void main(String[] args)
{
IndexProcesser processor = new IndexProcesser();
processor.createIndex( " d:/textfolder " );
}
{
// 成员变量,存储创建的文件存放位置
private String INDEX_STORE_PATH = " d:/index " ;
public void createIndex(String inputDir)
{
try
{
// 以MMAnalyzer作为分词工具创建一个IndexWriter
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH,
new MMAnalyzer(), true );
File filesDir = new File(inputDir);
// 取得所有需要建立索引的文件组
File[] files = filesDir.listFiles();
int length = files.length;
for ( int i = 0 ;i < length;i ++ )
{
String fileName = files[i].getName();
// 判断文件名是否为.txt文件
if (fileName.substring(fileName.lastIndexOf( " . " )).equals( " .txt " ))
{
Document doc = new Document();
// Field以后在学,Field.Index.TOKENIZED表示先分词后索引
Field field = new Field( " filename " ,files[i].getName(),Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
field = new Field( " content " ,loadFileToString(files[i]),Field.Store.NO,Field.Index.TOKENIZED);
doc.add(field);
writer.addDocument(doc);
}
}
writer.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String loadFileToString(File file)
{
try {
BufferedReader br = new BufferedReader( new FileReader(file));
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while (line != null )
{
sb.append(line);
line = br.readLine();
}
br.close();
return sb.toString();
}
catch (IOException e){
e.printStackTrace();
return null ;
}
}
public static void main(String[] args)
{
IndexProcesser processor = new IndexProcesser();
processor.createIndex( " d:/textfolder " );
}
写这个程序特另我郁闷,一直出现空指针错误,file.length一直没有值,查了很多资料都没结果,最后发现路径写错了,d://textfolder而不是d://testfolder,差点急死我啊,幸好找到了,^_^!