How to read the .doc/ .docx file in Java Program

How to read the .doc/ .docx file in Java Program

Hi,

I am beginner in Java programming language. Can anybody explain How to read .doc file in Java language. Please provide any online reference so that i try it.

Regards,

View Answers

February 25, 2011 at 11:08 AM

Hi,

In java programming language we normally using the POI Library to read the word document file. For doing this we will make class HWPFDocument which throw all of the Word file data and the class WordExtractor extract the text from the Word Document. The method getParagraphText() of WordExtractor class get the text from the word file as an array and display the data on word file. This way you can read the .doc or .docx file in Java programming language.

Thanks


August 30, 2012 at 6:52 PM

package doc.reader;

import org.apache.poi.poifs.filesystem.*; import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hwpf.*; import org.apache.poi.hwpf.extractor.*; import org.apache.poi.hwpf.usermodel.HeaderStories;

import java.io.*;

public class ReadDocFileFromJava {

public static void main(String[] args) {
    /**This is the document that you want to read using Java.**/
    String fileName = "d\\Resume.doc";

    /**Method call to read the document (demonstrate some useage of POI)**/
    readMyDocument(fileName);

}
public static void readMyDocument(String fileName){
    POIFSFileSystem fs = null;
    try {
        fs = new POIFSFileSystem(new FileInputStream(fileName));
        HWPFDocument doc = new HWPFDocument(fs);

        /** Read the content **/
        readParagraphs(doc);

        int pageNumber=1;

        /** We will try reading the header for page 1**/
        readHeader(doc, pageNumber);

        /** Let's try reading the footer for page 1**/
        readFooter(doc, pageNumber);

        /** Read the document summary**/
        readDocumentSummary(doc);

    } catch (Exception e) {
        e.printStackTrace();
    }
}   

public static void readParagraphs(HWPFDocument doc) throws Exception{
    WordExtractor we = new WordExtractor(doc);

    /**Get the total number of paragraphs**/
    String[] paragraphs = we.getParagraphText();
    System.out.println("Total Paragraphs: "+paragraphs.length);

    for (int i = 0; i < paragraphs.length; i++) {

        System.out.println("Length of paragraph "+(i +1)+": "+ paragraphs[i].length());
        System.out.println(paragraphs[i].toString());

    }

}

public static void readHeader(HWPFDocument doc, int pageNumber){
    HeaderStories headerStore = new HeaderStories( doc);
    String header = headerStore.getHeader(pageNumber);
    System.out.println("Header Is: "+header);

}

public static void readFooter(HWPFDocument doc, int pageNumber){
    HeaderStories headerStore = new HeaderStories( doc);
    String footer = headerStore.getFooter(pageNumber);
    System.out.println("Footer Is: "+footer);

}

public static void readDocumentSummary(HWPFDocument doc) {
    DocumentSummaryInformation summaryInfo=doc.getDocumentSummaryInformation();
    String category = summaryInfo.getCategory();
    String company = summaryInfo.getCompany();
    int lineCount=summaryInfo.getLineCount();
    int sectionCount=summaryInfo.getSectionCount();
    int slideCount=summaryInfo.getSlideCount();

    System.out.println("---------------------------");
    System.out.println("Category: "+category);
    System.out.println("Company: "+company);
    System.out.println("Line Count: "+lineCount);
    System.out.println("Section Count: "+sectionCount);
    System.out.println("Slide Count: "+slideCount);

}

}

Ads









Related Tutorials/Questions & Answers:
How to read the .doc/ .docx file in Java Program
How to read the .doc/ .docx file in Java Program  Hi, I am beginner in Java programming language. Can anybody explain How to read .doc file in Java.... This way you can read the .doc or .docx file in Java programming language. Thanks
read doc and docx file in javascript
read doc and docx file in javascript  How i read doc and docx file in javascript
Advertisements
serch document from .doc and .docx file
serch document from .doc and .docx file  how to find particular word from .doc and .docx file and that date insert into a data base
About search and replace contains of .doc or .docx file using java.
About search and replace contains of .doc or .docx file using java.  Hi , I am trying to replace contents of ms.word document in .doc or .docx... any one tell me how could I replace them using java
how read data from doc file in same formate in jsp
how read data from doc file in same formate in jsp  how we can read and display data on jsp page, from doc file with the same formatting
I have one small doubt , does this code read Microsoft Office suit file format data such as .doc , .docx etc ?
I have one small doubt , does this code read Microsoft Office suit file format data such as .doc , .docx etc ?  import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFileExample
How to Read file line by line in Java program
by line in Java. But there are various ways that can help read a larger file...()?. The output of read file line by line in Java is printed on console... of Java API is another way to read a file line by line in Java. It used
How to Read a File in Java
How to Read a File in Java? In this section we are going to know, How to read a file in Java. We have to follow three step to read a File. First get... can be very inefficient. Example Here is the program to read a file in Java
How To Read File In Java
How To Read File In Java In this section we will discuss about about how data.... Example This example demonstrates that how to read a file in java. To read a file... a Java Program to read the data of a file and used the FileInputStream class
How to read file in java
How to read file in java Java provides IO package to perform reading and writing operations with a file. In this section you will learn how to read a text... to read file in Java?" Another way of reading a file: The another
How To Read File In Java with BufferedReader
How To Read File In Java with BufferedReader class - example code This tutorial shows you how you can read file using BufferedReader class in your program... java.io.*; /** * How To Read File In Java with BufferedReader class
how to convert doc file into html using java swing
how to convert doc file into html using java swing  i want to convert doc file into html and display it on jtextarea please help me and give the sample code
How to read properties file in java
How to read properties file in java Description of Example:- In this example... of: "How to read properties file in Java?" How we will create...= 8285279608 Example of Read properties file in java import java.io.
doc file - Java Beginners
doc file   i want to read .doc file ?....it is a part of my project plz help. if possible , can anyone give me the code
Java program to read a text file and write to another file
How to Read file line by line in Java program...Java program to read a text file and write to another file - Creating... and we want to copy the content into another text file from our Java program
how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java
How to read properties file in Java?
the java.util.Properties class for reading a property file in Java program.... Here is the video tutorial of reading a property file in Java: "How to read properties file in Java?" Here is the data.properties the properties
how to read file using InputStreamReader in java
how to read file using InputStreamReader in java  Hi, I want to learn to use the InputStreamReader class of Java and trying to read a text file with the class. how to read file using InputStreamReader in java? Thanks  
how to read a text file with scanner in java
how to read a text file with scanner in java  Hi, I am looking for the example code in Java for reading text file line by line using the Scanner class. how to read a text file with scanner in java? Thanks   Hi
How to read big file line by line in java?
Learn how to write a program in java for reading big text file line by line... to read the big file in your java program. For example you have to process some... to read character files easily in Java program. Constructor takes file name
How to check if a file is read-only in java
How to check if a file is read-only in java A read-only file is any file... file: Java API provides the functions to find if any file is read only... will not be able to make changes to it. You can make a file read-only by using
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
Program to read the text from a file and display it on a JFrame.
Program to read the text from a file and display it on a JFrame.  import javax.swing.*; import java.io.*; import java.lang.*; import java.awt.*; class MegaViewer1 extends JFrame { JTabbedPane jtp1=new JTabbedPane
java read file
java read file  Hello i need some help... i want to read an MS Excel file in java so how to read that file
Read file in java
Read file in java  Hi, How to Read file in java? Thanks   Hi, Read complete tutorial with example at Read file in java. Thanks
Java read binary file
Java read binary file  I want Java read binary file example code... at Reading binary file into byte array in Java. Thanks   Hi, There is many more examples at Java File - Learn how to handle files in Java with Examples
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
How to read a large text file line by line in java?
How to read a large text file line by line in java?  I have been assigned a work to read big text file and extract the data and save into database... you kind advice and let's know how to read a large text file line by line in java
How do I read a large file quickly in Java?
How do I read a large file quickly in Java?  Hi, I my project I have... in 1-2 GB in size. What is the best way to read the file efficiently? How do I read a large file quickly in Java? Thanks   Hi, You can't read the whole
How to make a file read-only in java
How to make a file read-only in java A read-only file is any file... program will make a the file read only To make file read only file.setReadOnly(); method is used. In the page you learned how to make a file read only
How can i read a file from nth line onwards in java
How can i read a file from nth line onwards in java  Hi, I need to read a file content from its nth line onwards till end. How can i implement it in java. Regards, Chinnu

Ads