I'm studying these days java io Flowing things , A netizen asked a question after seeing the blog , That means his doc Why is the document dead or alive in the way I say it is messy .

At first I thought it was his method , The results of their own after trying to find and his results are also garbled .

So I searched the Internet for a while before I found out doc Documentation and excel You can't use ordinary ones io Stream method to read , It also needs to be used poi, So after a few attempts , Finally, I read this in the correct encoding format doc file .

Found in the process of online search doc and docx The method of reading is different , So I learned it by the way docx Simple reading of files .

One , Guide Package :
doc File reading , Import required poi-scratchpad Of jar Packages and dependent packages :

docx File read , Import required poi-ooxml Of jar Packages and dependent packages :

I use it maven Build project , The dependent packages are imported automatically ,maven The guide package is configured as follows :
<dependency> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> <version>3.8</version> </dependency>
<dependency> <groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId> <version>3.8</version> </dependency>
Two , Read the code of the file :
1,doc Simple example of file reading :
public static void readAndWriterTest3() throws IOException { File file = new
File("C:\\Users\\tuzongxun123\\Desktop\\aa.doc"); String str = ""; try {
FileInputStream fis = new FileInputStream(file); HWPFDocument doc = new
HWPFDocument(fis); String doc1 = doc.getDocumentText();
System.out.println(doc1); StringBuilder doc2 = doc.getText();
System.out.println(doc2); Range rang = doc.getRange(); String doc3 =
rang.text(); System.out.println(doc3); fis.close(); } catch (Exception e) {
e.printStackTrace(); } }
2,docx Simple example of file reading :
public static void readAndWriterTest4() throws IOException { File file = new
File("C:\\Users\\tuzongxun123\\Desktop\\aa.docx"); String str = ""; try {
FileInputStream fis = new FileInputStream(file); XWPFDocument xdoc = new
XWPFDocument(fis); XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
String doc1 = extractor.getText(); System.out.println(doc1); fis.close(); }
catch (Exception e) { e.printStackTrace(); } }
//20171218 modify
I didn't do it at work word, This blog is only done on the spur of the moment , So it's very simple .
Recently, some friends came to me to ask me some questions , Several of them are asking what are the dependent packages , To avoid answering this question again and again , Will rely on the package screenshot :

©2019-2020 Toolsou All rights reserved,
Send love - A little romance for programmers VHDL—— Design of frequency divider Python Implementation of Hanoi Tower code It's over , Starting salary 30khtml+css+js Make a simple website home page QQ Login interface implementation Hill sorting of sorting algorithm ——c++ realization 【 Wechat applet learning 】 Netease music cloud code page implementation details Resume the 13th session python Blue Bridge Cup 2022 Solution to the 13th Blue Bridge Cup ( whole )