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);

}

}









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... how could I replace them using java
How to read a line from a doc file
How to read a line from a doc file  How to read a line from a doc file   Hi Friend, To run the following code, you need to download POI... { public static void main(String[] args) { File file = null; WordExtractor
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 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
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... in example ?inputStream.close()?. The output of read file line by line in Java... class of Java API is another way to read a file line by line in Java. It used
Program to read a dynamic file content - Java Beginners
Program to read a dynamic file content  Hi, In a single... the database. Im using MySql Database and a standalone java program. I...", "020209.txt", "030209.txt","040209.txt". In a single file I'll get the datas
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...({}); Example Here is the program to read a file in Java. import java.io.*; public
how to store/retrieve doc file - Java Beginners
how to store/retrieve doc file  i want to wirte a code that stores/ retrieves .doc files to the mysql database using jsp pages... can anyone help me with the code?  Use this stuff inside your jsp page for store file
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
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 File In Java
How To Read File In Java In this section we will discuss about about how data of a file can be read in Java. A file can contain data as bytes, characters... demonstrates that how to read a file in java. To read a file there must
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...*; /** * How To Read File In Java with BufferedReader class * */ public class
How to read properties file in java
How to read properties file in java Description of Example:- In this example... to read properties file in Java?" How we will create .properties file... read properties file;- (adsbygoogle = window.adsbygoogle || []).push
Java Read .doc file using POI library
Java Read .doc file using POI library In this section, you will learn how to read the word document file using POI library. The class HWPFDocument throw all of the Word file data and the class WordExtractor extract the text from
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?
of reading a property file in Java: "How to read properties file in Java... the java.util.Properties class for reading a property file in Java program...Example program of reading a properties file and then printing the data
Java program to read a text file and write to another file
Java program to read a text file and write to another file - Creating... How to Write to a File in Java without overwriting? Java read... and we want to copy the content into another text file from our Java program
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 an eml file? - Java Beginners
How to read an eml file?  Hello dears, now i want to read en eml file stored in a folder. is it possible to use the same code for reading a txt file? or do i have to use javamail api? Thanks Spalax
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 In this tutorial I will explain you how you can read big file line by line... to read the big file in your java program. For example you have to process some
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... to check read only file: Java API provides the functions to find if any file... 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
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 (adsbygoogle = window.adsbygoogle || []).push({});   Hi, Read complete tutorial with example at Read file in java. Thanks (adsbygoogle
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 binary file
Java read binary file  I want Java read binary file example code... = window.adsbygoogle || []).push({}); There is many more examples at Java File - Learn how to handle files in Java with Examples and Tutorials. Thanks
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 to read this xml file - XML
how to read this xml file  i want to read this xml file using java... read i have tried lot more , but i am not able to read this xml file... name=client menu=client action=read user employee add
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... memory and the program works very fast. Check the tutorial: Java Read File Line... in 1-2 GB in size. What is the best way to read the file efficiently? How do I read
How to read the data in text file seperated by by ',' in java using IO Operations
How to read the data in text file seperated by by ',' in java using IO Operations  in Text file data like raju 45,56,67 ramu 46,65,78 raji..._TO_REPLACE_1 raji fail how to write the program
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 onlyADS_TO_REPLACE_1 To make file read only... a file read-only by using file.setReadOnly() method of File class. After using
Convesion of txt file to doc file.??????
Convesion of txt file to doc file.??????  how to convert text file to doc file using java
How to check a file is read-able or not in java
Description: This example demonstrate how to check a file have read permission... check for its read property of a file. As in the file path we see '\'... or false depends on the file have read property at the specified path that is set
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
how to read text file with java 8 stream api
how to read text file with java 8 stream api  Hi, I want to use Java... code. how to read text file with java 8 stream api? Thanks   Hi, Following example is for reading text file line by line in Java using the stream api
how to read file line by line using filereader in java
how to read file line by line using filereader in java  Hi, how to read file line by line using filereader in java? Thanks   Hi, This is example code: package my.code; import java.io.BufferedReader; import
JDOM example in java, How to read a xml file in java.
JDOM example in java, How to read a xml file in java. In this tutorial, we will see how to  read a xml file in java. Code. Student.xml...;File(data);       DOMBuilder builder 
How to Read Excel file Using Java
How to Read Excel file In this section,you will learn how to read excel file... and every cell and stored the excel file values into the vector.This Vector data is then used to display file values on the console. Here is the code
Java read file line by line - Java Tutorial
; This program reads the bytes from file and display it to the user. Download...; C:\nisha>java FileRead this is a file...C:\nisha>javac ReadFile.java C:\nisha>java
Java - Read file Applet
Java - Read file Applet     ... the concept of the reading file from an applet. This program illustrates you how an applet can read the content from the given file. In this program we passes
Read File in Java
Read File in Java This tutorial shows you how to read file in Java. Example... java.io.*; /** * This example code shows you how to read file in Java * */ public... to read the character files. This class takes file name as a parameter
Merge multiple jasper file to one word Doc using java
Merge multiple jasper file to one word Doc using java   how to Merge multiple jasper file to one word Doc using java
Read File from specified path in Java
How to read a file from a specified path in Java? After learning so many examples of file handling in  Java, we will show to a simple program which... and file name to the program and program will read content of file. After opening
Java read properties file
Java read properties file In this section, you will learn how to read... Video tutorial of reading properties file in Java: "How to read properties file in Java?" Here is the code: import java.io.*; import

Ads