Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read .doc file using POI Library

 
Vicky Thakor
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic