forked from mood321/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocUtil.java
More file actions
70 lines (63 loc) · 2.03 KB
/
Copy pathDocUtil.java
File metadata and controls
70 lines (63 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package Test;
/**
* @author mood321
* @date 2021/9/1 22:00
* @email [email protected]
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class DocUtil {
public static String doc2String(File file) throws IOException {
String result=new String("");
if(file.getName().toLowerCase().endsWith("docx")){
return result=docString(new FileInputStream(file));
} else {
result=doc2String(new FileInputStream(file));
}
return result.replace("","");
}
/**
* 读取doc文件内容
*
* @param fs
* 想要读取的文件对象
* @return 返回文件内容
* @throws IOException
*/
public static String doc2String(FileInputStream fs) throws IOException {
StringBuilder result = new StringBuilder();
WordExtractor re = new WordExtractor(fs);
result.append(re.getText());
re.close();
return result.toString();
}
public static String docString(FileInputStream fis) throws IOException {
XWPFDocument xdoc = new XWPFDocument(fis);
XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
String doc1 = extractor.getText();
fis.close();
return doc1;
}
public static void main(String[] args) {
File file = new File("");
try {
System.out.printf(file.getPath());
System.out.printf(file.getName());
File file1 = new File("D:\\2.md");
String s = doc2String(file);
System.out.println(s);
FileOutputStream fos = new FileOutputStream(file1) ;
doc2String(file);
fos.write(s.getBytes());
fos.close();
System.out.println("1");
} catch (Exception e) {
e.printStackTrace();
}
}
}