OCJP

Read doc File and docx file in Java Code

This is sample code to Read Microsoft Word File in doc format and docx format using Java Code.

You have to attach proper Jar Files

public static void readDocxFile(String path) {
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());

XWPFDocument document = new XWPFDocument(fis);

List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph para : paragraphs) {
System.out.println(para.getText());
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void readDocFile(String path)
{
WordExtractor extractor = null ;
try {

File file = new File(path);
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
HWPFDocument document=new HWPFDocument(fis);
extractor = new WordExtractor(document);
String [] fileData = extractor.getParagraphText();
for(int i=0;i<fileData.length;i++){
if(fileData[i] != null)
{
//txtdocs.append(fileData[i]+"\n");
System.out.println(fileData[i]);
}
}
}
catch(Exception exep){

exep.printStackTrace();
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?