Professional Documents
Culture Documents
Comdoc Algorithms: 1. Read Simple Data From
Comdoc Algorithms: 1. Read Simple Data From
1 package com.kscodes.test;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5
6 import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
7 import org.apache.poi.xwpf.usermodel.XWPFDocument;
8
9 public class ReadDocUsingPOI {
10 public static void main(String args[]) {
11
12 XWPFDocument document = null;
FileInputStream fileInputStream = null;
13
try {
14
15
16
File fileToBeRead = new
17
File("C:\\kscodes_temp\\SimpleFileToRead.docx");
18
fileInputStream = new FileInputStream(fileToBeRead);
19
document = new XWPFDocument(fileInputStream);
20
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
21
22
System.out.println("The Contents of the Word File are ::");
23
System.out.println("--------------------------------------");
24
25
System.out.println(extractor.getText());
26
27
} catch (Exception e) {
28
System.out.println("We had an error while reading the Word Doc");
29
} finally {
30
try {
31
if (document != null) {
32
document.close();
33
}
34
if (fileInputStream != null) {
35
fileInputStream.close();
36
}
37
} catch (Exception ex) {
38
}
39
}
40
41
}
42
}
Output
As you can see that the XWPFWordExtractor.getText() will always return simple String that it reads.