Could someone tell me whether it is possible to check if a file is being read. If it is being read then how do I make my program wait, till the reading is over so that it can begin writing into it.
I got the following error when I tried to write into a file that was being read:
net.sf.jasperreports.engine.JRException: Error trying to export to file :
......(some errors not shown here) ....
Caused by: java.io.FileNotFoundException: D:\servers_insta开发者_JAVA技巧lls\tomcat6_9494\webapps\testdoc\logs\xyz.pdf (The process cannot access the file because it is being used by another process)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at net.sf.jasperreports.engine.export.JRPdfExporter.exportReport(JRPdfExporter.java:305)
... 19 more
I haven't used Java in a while, but one option I believe you have is to use try catch blocks. If an exception is caught, that means that writing failed, and you can keep trying again until it works. Here's an example that may or may not be correct Java code. But you should get the general idea.
boolean worked = false;
while (!worked && timeout<20)
{
try
{
// writing code here
// set worked to true
worked = true;
}
catch
{
// do nothing
}
}
Make sure this is running in a separate thread. And also time it out in case there's another issue that's preventing the write from happening. Hope that helps.
You don't have to check if a file is being read:
Call the read function.
Call the write function.
Calling the functions in that order will automatically have the program FIRST read the file and then WRITE TO the file.
The following code may help:
try
{
//enter the code which is giving the specified error here.
}
catch(JRException e)
{
System.out.println("Jasper Exception.");
}
Make sure all your variables have been initialized correctly. If you can post your code up here that would really help by the way.
精彩评论