Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

CodeSharing
CodeSharing

Posted on

     

Java/ Extract Text from a Word Document

TXT is a common text format that can be used on many computers and mobile devices. The TXT document is known for its small size, and it makes the storage of text content more convenient. This article will demonstrate how to extract the text content in a Word document and save it as .txt format by using Free Spire.Doc for Java.

Import JAR Dependency to Your Java Application
Method 1: Download theFree Spire.Doc for Java and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url>http://repo.e-iceblue.com/nexus/content/groups/public/</url></repository></repositories><dependencies><dependency><groupId>e-iceblue</groupId><artifactId>spire.doc.free</artifactId><version>3.9.0</version></dependency></dependencies>
Enter fullscreen modeExit fullscreen mode

Extract Text

importcom.spire.doc.Document;importjava.io.FileWriter;importjava.io.IOException;publicclassExtractText{publicstaticvoidmain(String[]args)throwsIOException{//Load Word documentDocumentdocument=newDocument();document.loadFromFile("Island.docx");//Get text from document as stringStringtext=document.getText();//Write string to a .txt filewriteStringToTxt(text," Extracted.txt");}publicstaticvoidwriteStringToTxt(Stringcontent,StringtxtFileName)throwsIOException{FileWriterfWriter=newFileWriter(txtFileName,true);try{fWriter.write(content);}catch(IOExceptionex){ex.printStackTrace();}finally{try{fWriter.flush();fWriter.close();}catch(IOExceptionex){ex.printStackTrace();}}}}
Enter fullscreen modeExit fullscreen mode

Extract text

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Sharing code to help developers deal with their files in Java applications.
  • Joined

More fromCodeSharing

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp