Free Support Forum -

Error opening file in multi threaded java application

Hi! I am trying to retrieve all documents from different folders through a multi-threaded Aspose word java application. But I get ‘FileCorruptedException’ in some documents. Kindly help me. Thanking you in anticipation

‘FileCorruptedException’ occurs randomly. I mean the documents keeps on changing. It doesn’t occur on a specific set of documents.


Thanks for your request. Please provide me the code that will allow me to reproduce this problem.

Also make sure that you are using separate Document object per each thread. One thread should use one Document object.

Best regards.

ProcessDocuments is my class which is implementing runnable interface.Following is the summay of my code

ProcessDocuments processDocuments = new ProcessDocuments();

for(int i=0;i<THREAD_COUNT;i++)
processDocuments = new ProcessDocuments();
thread = new Thread(processDocuments);

My Run Method is Executing following method

private void makeTheDocumentReadOnlyAndInsertWatermark(String fileName,String sourceDirectory,String destDirectory,int fileNo,int totalFiles)
int currentFile = fileNo+1;
System.out.println(“File Name : “+fileName+”\nFolder : “+sourceDirectory+”\nFile : “+currentFile+”/”+totalFiles+"\n");

System.out.println(fileName + " " + Thread.currentThread().getName());
Document doc = new Document(sourceDirectory+fileName); //exception occur here
DocumentBuilder builder = new DocumentBuilder(doc);
insertWaterMark(doc,builder,“APPROVED”); // my method to insert watermark;
catch(Exception e)
System.out.println(“Name = " + Thread.currentThread().getName() + " Filename” + fileName);



The ctor ‘new Document(String filename)’ opens a template file read-only and normally shouldn’t cause such an exception (if you don’t modifying the template doc in some another thread). So, please check did you modifying the template doc in any thread (including open Word editor).

Also please provide me your full stack trace - com.aspose.words.FileCorruptedException in this ctor wraps a range of different exceptions from IOException to NullPointerException – but we should to know the real cause.

And at last, for workaround you can try another Document ctor: Document(InputStream stream) instead of Document(String filename). The last one uses RandomAccessFile internally – may be there are some conflicts occur in your specific environment when you are using this class in multithread.

Best regards,