Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners 16686-How-to-read-the-.doc How to read the .doc/ .docx file in Java Program
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Nadeem Kumar
How to read the .doc/ .docx file in Java Program
2 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners


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,


Advertisement
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
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
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
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 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... void main(String[] args) { File file = null; WordExtractor extractor = null
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 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 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
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 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
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 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 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
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 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
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
How to read properties file in java
How to read properties file in java Description of Example:- In this example...= 8285279608 Example of Read properties file in java import java.io.... read properties file;- ResourceBundle. Properties 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 file line by line  in java. FileInputStream- This class reads bytes from
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 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... to value. The java.util.Properties class is used to read the properties file
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
Reading .doc file using swing
Reading .doc file using swing  Sir, Could U Please Tell me the Way To Read .doc file in java using swing,with code
Write data in doc file
Write data in doc file  How to write data or string in word file
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
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
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
 

 

 

DMCA.com