- Java 程序设计示例
- 示例 - Home
- 示例 - Environment
- 示例 - Strings
- 示例 - Arrays
- 示例 - Date & Time
- 示例 - Methods
- 示例 - Files
- 示例 - Directories
- 示例 - Exceptions
- 示例 - Data Structure
- 示例 - Collections
- 示例 - Networking
- 示例 - Threading
- 示例 - Applets
- 示例 - Simple GUI
- 示例 - JDBC
- 示例 - Regular Exp
- 示例 - Apache PDF Box
- 示例 - Apache POI PPT
- 示例 - Apache POI Excel
- 示例 - Apache POI Word
- 示例 - OpenCV
- 示例 - Apache Tika
- 示例 - iText
- Java 教程
- Java - 教程
- Java 有用资源
- Java - 快速指南
- Java - 有用资源
如何使用 Java 从文本文档中提取内容
问题描述
如何使用 Java 从文本文档中提取内容。
解决方案
下面是使用 Java 从文本文档中提取内容的程序。
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 ExtractContentFromTextDoc {
public static void main(String[] args) throws Exception {
//detecting the file type
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(new File(
"C:/tika/textExample.txt"));
ParseContext pcontext = new ParseContext();
//Text document parser
TXTParser TexTParser = new TXTParser();
TexTParser.parse(inputstream, handler, metadata,pcontext);
System.out.println("Contents of the document:" + handler.toString());
System.out.println("Metadata of the document:");
String[] metadataNames = metadata.names();
for(String name : metadataNames) {
System.out.println(name + " : " + metadata.get(name));
}
}
}
输入
输出
Contents of the document: Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more. Metadata of the document: Content-Encoding : windows-1252 Content-Type : text/plain; charset = windows-1252
java_apache_tika
广告