24 Sept 2012

Converting Document into Text file in java

/* Converting Document into Text file in java code, Convert .doc file to .txt file, how to convert Document into Text file in java code*/

import java.io.*;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class ReadDocFile 
{
public static void main(
java.lang.String[] args) 
{ java.io.File file = null;

try 
{
// Read the Doc/DOCx file
file = new 
java.io.File("D:\\New.docx"); 
java.io.FileInputStream fis = new java.io.FileInputStream(file.getAbsolutePath()); 
XWPFDocument doc = new XWPFDocument(fis);
XWPFWordExtractor ex = new XWPFWordExtractor(doc);
String text = ex.getText();

//write the text in txt file
java.io.File fil = new java.io.File("D:\\New.txt"); 
java.io.Writer output = new java.io.BufferedWriter(new java.io.FileWriter(fil)); 
output.write(text);
output.close();
catch (java.lang.Exception exep) 
{
}
}
}


Also upload the xmlbeans-2.3.0,dom4j-1.6.1 and stax-api-1.0.1.

Download the Apache POI jar also.