Index is required first for search. Index TXT files are the easiest way to search. Here we will introduce the indexes of documents in other formats, such as MS Word, PDF, and RTF.
The indexing method is to convert all kinds of documents into plain text before indexing, so the key lies in the conversion. Fortunately, there are too many open source projects in the Java World, and many of them can be used directly. The following sections describe:
Before writing it all: All the is parameters described below are inputstream, which is the file to be indexed.
Word documents:
Open-source projects that convert Word documents into plain text can be used: poi or textmining.
Poi usage: Worddocument WD = New Worddocument (is );
Stringwriter doctextwriter = New Stringwriter ();
WD. writealltext ( New Printwriter (doctextwriter ));
Doctextwriter. Close ();
Bodytext = Doctextwriter. tostring ();
Textmining is easier to use:Bodytext= NewWordextractor (). extracttext (is );
PDF:
The class library that can be used to convert PDF files is product_box. Cosdocument cosdoc = NULL;
Partition parser = new partition Parser (is );
Parser. parse ();
Cosdoc = Parser. getdocument ()
If (Cosdoc. isencrypted ()){
Decryptdocument decryptor = New decryptdocument (cosdoc );
Decryptor. decryptdocument (password );
}
Pdftextstripper Stripper = New vertex textstripper ();
String doctext = Stripper. gettext (New pddocument (cosdoc ));
RTF document:
The conversion of RTF is available in javax.Defaultstyleddocument styleddoc= NewDefaultstyleddocument ();
NewRtfeditorkit (). Read (is, styleddoc,0);
String bodytext=Styleddoc. gettext (0, Styleddoc. getlength ());
In this way, you can index texts in various formats.
The Processing Method for HTML and XML is the same.
The difference is that the available HTML class libraries are: jtidy
The available XML class libraries are Sax and digester.