I am using the Transformer class in java in the following way -
我以下列方式在java中使用Transformer类 -
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 which was used for the transforming.
然后我想删除用于转换的源文件。
4 File fileToDelete = new File(tempFilename);
5 fileToDelete.delete();
It doesnt work, I mean the file doesnt get deleted.
But if at line 3
I pass a local variable of the o/p stream viz.
它不起作用,我的意思是该文件不会被删除。但是如果在第3行,我传递了o / p流的局部变量。
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 does delete the file.
So, am I correct, when I conclude that the o/p stream is not closed implicitly during the transform
process ? and hence I have to close the stream explicitly.
Can anyone please share, if there is any other reason the file may not be getting deleted?
现在删除功能工作,它确实删除了文件。所以,我是否正确,当我得出结论,在转换过程中o / p流没有隐式关闭?因此我必须明确地关闭流。任何人都可以分享,如果有任何其他原因该文件可能不会被删除?
Please assume that all the variables have correct values.
请假设所有变量都具有正确的值。
Thank You.
Update
One more thing I noticed.
I am calling this code from an another class, eg. -
还有一点我注意到了。我从另一个类调用此代码,例如。 -
public class ClassTwo {
public void ameth(String tempFilename) {
// the above mentioned transformation code
}
}
1 public class ClassOne {
2 public void method1() {
3 ClassTwo ct = new ClassTwo();
4 ct.ameth("tempFilename1");
5 ct.ameth("tempFilename2");
6 }
7 }
Here, When I didn't explicitly close the stream, it did delete the tempFilename2
but not the tempFilename1
.
Any idea, why it behaves so?
这里,当我没有显式关闭流时,它确实删除了tempFilename2而不是tempFilename1。任何想法,为什么它表现如此?
1 个解决方案
#1
5
You're right: You can't delete a file that is still open. Due to an old bug in the Java API, the delete()
can't tell you why -- it can only return a boolean
result.
你是对的:你不能删除仍然打开的文件。由于Java API中存在旧错误,delete()无法告诉您原因 - 它只能返回布尔结果。
The reason for this behavior is that Java can't automatically clean up system resource other than heap memory. So we end up with the problem: Who can safely close the file? Maybe the transformation isn't complete, yet. Or you need to write a header+footer in the same stream.
这种行为的原因是Java无法自动清理堆内存以外的系统资源。所以我们最终遇到了问题:谁可以安全地关闭文件?也许转型尚未完成。或者您需要在同一个流中编写标题+页脚。
So if you create a stream, you always have to close it.
因此,如果您创建流,则始终必须关闭它。
#1
5
You're right: You can't delete a file that is still open. Due to an old bug in the Java API, the delete()
can't tell you why -- it can only return a boolean
result.
你是对的:你不能删除仍然打开的文件。由于Java API中存在旧错误,delete()无法告诉您原因 - 它只能返回布尔结果。
The reason for this behavior is that Java can't automatically clean up system resource other than heap memory. So we end up with the problem: Who can safely close the file? Maybe the transformation isn't complete, yet. Or you need to write a header+footer in the same stream.
这种行为的原因是Java无法自动清理堆内存以外的系统资源。所以我们最终遇到了问题:谁可以安全地关闭文件?也许转型尚未完成。或者您需要在同一个流中编写标题+页脚。
So if you create a stream, you always have to close it.
因此,如果您创建流,则始终必须关闭它。