This is a test version of Biostars. For the public version, visit https://www.biostars.org.
VCFFileReader doesn't close

I am using htsjdk.variant.vcf.VCFFileReader class to parse .vcf document, so I've got a constructor

VCFFileReader vcfReader = new VCFFileReader(inputFile, false);

and some code like

        for (VariantContext variantContext : vcfReader) {
           //..         
        }
vcfReader.close()

Unfortunately, my file isn't released after I close VCFReader (even if it is an empty cycle), but in construction like

VCFFileReader vcfReader = new VCFFileReader(inputFile, false);
vcfReader.close();

Everything is ok. Does anybody know how to solve it?

Thanks

htsjdk java

Are you sure the code gets to vcfReader.close()?

how do you know that the file isn't realeased ? an exception ? what's in the loop ?

I can't delete my file after vcfReader.close()

Are you sure nothing else is accessing it? What does lsof say?

1 answer

To be sure that your Reader is closed, use a try/finally statement

VCFFileReader in=null;
try {
in=new VCFFileReader(inputFile, false);
for(VariantContext ctx: in)
   {
   /* something */
   }
}
finally
{
htsjdk.samtools.util.CloserUtil.close(in);
}

Thanks for your reply, but I still can't delete file even after this construction.

Log in to answer this question.