I am using the Transformer class in java as follows -
1 Transformer transformerFinal = tFactory.newTransformer(new StreamSource(finalStylesheet)); 2 transformerFinal.setParameter("Date", sdf.format(myDate)); 3 transformerFinal.transform(new StreamSource(tempFilename), new StreamResult(new FileOutputStream(finalFilename)));
And then I want to delete this source file that was used for conversion.
4 File fileToDelete = new File(tempFilename); 5 fileToDelete.delete();
It does not work, I mean that the file is not deleted.
But if at line 3 , I pass the local stream variable o / p, namely.
1 FileOutputStream fos = new FileOutputStream(finalFilename); 4 transformerFinal.transform(new StreamSource(tempFilename), new StreamResult(fos)); 5 fos.close();
Now the delete function works and it deletes the file.
So, is it right when I conclude that the o / p stream does not close implicitly during the transform process? and therefore I have to close the stream explicitly.
Can anyone share if there is any other reason why the file cannot be deleted?
Suppose all variables have the correct values.
Thanks.
Update
Another thing I noticed.
I am calling this code from another class, for example. -
public class ClassTwo { public void ameth(String tempFilename) {
Here, when I did not explicitly close the stream, it removed tempFilename2 , but not tempFilename1 .
Any idea why she is behaving this way?
source share