In this program, you will see the complete example to parse content and metadata of text file by using TIKA TXTParser.
Sample File

Complete Example
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.ParseContext; import org.apache.tika.sax.BodyContentHandler; import org.apache.tika.parser.txt.TXTParser; import org.xml.sax.SAXException; public class TikaTextParserExample { public static void main(final String[] args) throws IOException,SAXException, TikaException { //detecting the file type BodyContentHandler handler = new BodyContentHandler(); Metadata metadata = new Metadata(); FileInputStream inputstream = new FileInputStream(new File("C:\\Users\\Saurabh Gupta\\Desktop\\TIKA\\TEXT-FILE.txt")); ParseContext pcontext=new ParseContext(); //Text document parser TXTParser TexTParser = new TXTParser(); TexTParser.parse(inputstream, handler, metadata,pcontext); System.out.println("Contents of the text document:" + handler.toString()); System.out.println("Metadata of the text document:"); String[] metadataNames = metadata.names(); for(String name : metadataNames) { System.out.println(name + " : " + metadata.get(name)); } } }
Output
Contents of the text document:You are in FacingIssuesOnIT.
Learn from Others Experience.
Metadata of the text document:
Content-Encoding : windows-1252
Content-Type : text/plain; charset=windows-1252
You must log in to post a comment.