It's not a secret anymore!*
The moose likes Java in General and the fly likes Read .doc file using POI Library Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Read .doc file using POI Library" Watch "Read .doc file using POI Library" New topic
Author

Read .doc file using POI Library

Vicky Thakor
Greenhorn

Joined: Sep 04, 2010
Posts: 19
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
directory.createDocument("WordDocument", new ByteArrayInputStream(content.getBytes()));
FileOutputStream out = new FileOutputStream(FileName);

fs.writeFilesystem(out);
out.close();

i'm creating (.doc) file using the above code. File created using the above code is opening in MicrosoftWord. But when i'm trying to open the same file in Jtextarea using the below code

WordExtractor extractor = null;
tabcount = e.jTabbedPane1.getSelectedIndex();
try {
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
HWPFDocument document = new HWPFDocument(fis);
extractor = new WordExtractor(document);
String fileData = extractor.getText();

if (fileData != null) {
e.textAreas[tabcount].append(fileData);
}

} catch (Exception doc_error) {
System.out.println(doc_error);
}

It shows error : java.lang.ArrayIndexOutOfBoundsException: 4

but the file created using MicrosoftWord opened by above code.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
The error is not occurring in the POI code, it's in the Swing code, so it's not surprising that there are no problems opening the file in Word. You didn't say in which line the exception happens, but it's probably in "e.textAreas[tabcount]", since that's the only array being used. Apparently, the textAreas array does not have 5 elements, so trying to access element #4 won't work. (Remember that arrays are 0-based in Java, so at index 4 is the 5th element).
Vicky Thakor
Greenhorn

Joined: Sep 04, 2010
Posts: 19
Lester Burnham wrote:The error is not occurring in the POI code, it's in the Swing code, so it's not surprising that there are no problems opening the file in Word. You didn't say in which line the exception happens, but it's probably in "e.textAreas[tabcount]", since that's the only array being used. Apparently, the textAreas array does not have 5 elements, so trying to access element #4 won't work. (Remember that arrays are 0-based in Java, so at index 4 is the 5th element).



- I know that but as i said...The "doc" file created using POI library is not get open in the jTextarea but other "doc" file created using MicrosoftWord is opening in jTextarea
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I think you missed what I was trying to convey. The problem has nothing to do with handling a Word document, and also nothing to do with the POI library.

The problem is that the code is using an array index that is invalid. You still haven't told us in which line of code the exception happens, so I'm assuming it's in "e.textAreas[tabcount]". Apparently tabcount has a value of 4, while textAreas has a size of less than 5. That's where you should begin investigating.

On the other hand, if the exception happens in some other line of code, then it's high time you told us which line that is (and also posted the full stack trace of the exception).
Vicky Thakor
Greenhorn

Joined: Sep 04, 2010
Posts: 19
Lester Burnham wrote:I think you missed what I was trying to convey. The problem has nothing to do with handling a Word document, and also nothing to do with the POI library.

The problem is that the code is using an array index that is invalid. You still haven't told us in which line of code the exception happens, so I'm assuming it's in "e.textAreas[tabcount]". Apparently tabcount has a value of 4, while textAreas has a size of less than 5. That's where you should begin investigating.

On the other hand, if the exception happens in some other line of code, then it's high time you told us which line that is (and also posted the full stack trace of the exception).





The above error i'm getting....
 
 
subject: Read .doc file using POI Library
 
Similar Threads
Regarding .DOC file
java.io.FileNotFoundException
TIFF. DOC, EXCEL to PDF Converter
problem with reading text by POI Apache.
Reading Microsoft Word Document in JAVA