1

I am trying the code below to open and read a Word file in JTextArea, using Netbeans.

import java.io.*;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.extractor.WordExtractor;

public class ReadDocFile {
    public static void main(String[] args) {
        File file = null;
        WordExtractor extractor = null;

        try {
            file = new File("c:\\New.doc");
            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)
                    System.out.println(fileData[i]);
            }
        } catch (Exception exep) {
        }
    }
}

I get the error below:

cannot find symbol : HWPFDocument((mypackgename).fileinputstream )

Due to this, I can not open the file. Is there a reason that this isn't working?

Makoto
  • 104,088
  • 27
  • 192
  • 230
user3340988
  • 11
  • 1
  • 2
  • 1- your post and code suggest that you would like to read a pdf file. Yet you're feeding your code a word document **File = new File("c:\New.doc");** – b2Wc0EKKOvLPn Feb 22 '14 at 15:40
  • 2- your code suggest you're using a 3rd party library: **Apache POI-HWPF**. Are you sure you have that library in the classpath of your netbeans project? – b2Wc0EKKOvLPn Feb 22 '14 at 15:41
  • Possible duplicated -> http://stackoverflow.com/questions/7102511/how-read-doc-or-docx-file-in-java – slackmart Feb 22 '14 at 15:43
  • @sgmart: **The code from the linked "duplicate" is a 1:1 copy.** – Makoto Feb 22 '14 at 15:44
  • It's a copy but not a duplicate. It's copied from an an answer to the linked question (and possibly a wrong answer, since that question was about reading a word document and not a pdf file). – b2Wc0EKKOvLPn Feb 22 '14 at 15:48
  • 1
    Thing about this question: It *mentioned* reading a PDF document, but the code used to ask the question only concerned itself around Word documents. I leave it to the OP to clarify if they want to read either a PDF, a Word document, or both. – Makoto Feb 22 '14 at 15:49
  • @Makoto: yes I agree with you there, hence my first comment. – b2Wc0EKKOvLPn Feb 22 '14 at 15:50

0 Answers0