[jdom-interest] File lock after validation error

Terje Kvambe Terje.Kvambe at multimedialab.tv
Fri Sep 14 08:21:01 PDT 2001


Hi

I am using JDOM (beta 7) to read an XML file that is validated with a DTD
using the Saxbuilder:

File inputFile = new File( "myfile.xml" );
SAXBuilder builder = new SAXBuilder();
builder.setValidation( true );
try
{
	Document doc = builder.build( inputFile );
}
catch ( JDOMException ex ) 
{
	// handle the error and throw a new exception
}
finally
{
	//After parsing the document I want to delete the original file
	inputFile.delete();
}

But when the validation fails the file is NOT deleted...
Does anybody know why? 
It seems to be a lock on the file or something, either by JDOM or by the
JVM.
I've tried to delete the file in several different places in my code, but
the file is only "unlocked" when I restart the program. I can manually
delete or edit the file in windows, so the file is not read-only.

Could this be solved by using an InputStream in the build method instead of
File?
Well, I tried using an inputstream and it works, but only when I explicitly
close the InputStream after a validation error occurs:

File inputFile = new File( "myfile.xml" );
InputStream is = null;
SAXBuilder builder = new SAXBuilder();
builder.setValidation( true );
try
{
	is = new java.io.FileInputStream( inputFile );
	doc = builder.build( is );
}
catch ( JDOMException ex ) 
{
	// handle the error and throw a new exception
}
finally
{
	// Explicitly close the InputStream
	try
	{ 
		is.close()
	}
	catch (IOException ioe) {}
	//After parsing the document I want to delete the original file
	inputFile.delete();
}

Why do I have to do this? Is this a bug in JDOM? 

Best regards, 
Terje Kvambe
Accenture
Oslo, Norway




More information about the jdom-interest mailing list