如
有个方法f1()返回Instream,另一个方法f2(Instream)以Instream作为对象,这两个方法都是别人实现。使用这两个方法如下
myfun(){
Instream in=f1();
f2(in);
}
那关闭Instream是应在f1中还是f2()中或在myfun中的调用f2()后,用in.close();关闭?
如果myfun实现改为
myfun(){
f2(f1());
}
又如何了?
有个方法f1()返回Instream,另一个方法f2(Instream)以Instream作为对象,这两个方法都是别人实现。使用这两个方法如下
myfun(){
Instream in=f1();
f2(in);
}
那关闭Instream是应在f1中还是f2()中或在myfun中的调用f2()后,用in.close();关闭?
如果myfun实现改为
myfun(){
f2(f1());
}
又如何了?
搂主现在的情况下应该在myfun中(f2后面关闭)关闭流。
如规定在f中负责关闭in,那按下面方式调用
Instream in=new FileInstream(new File("xxx"));
f(in);
在调用方法f后,就不能再使用in了。
如规定在f中不负责关闭in,那就不能按下面方法调用了
f(new FileInstream(new File("xxx")));也就是说现在定义的接口还和调用此接口的方式有关,这样是不是太不灵活了?
jdom中向文件写入xml
org.jdom.output.XMLOutputter xmlouter= new org.jdom.output.XMLOutputter();
xmlouter.output(doc,new FileOutputStream("a.xml")); ---1其中1处就不能这么写,是吗?