下面是某本书上的一段程序代码,有个疑问如下,红色字体..
package day1;import java.io.ByteArrayInputStream;
import java.io.IOException;public class ByteArrayTester { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] buff=new byte[]{2,15,67,-1,-9,9};
ByteArrayInputStream in=new ByteArrayInputStream(buff,1,4);
int data=in.read();
while(data!=-1){
System.out.println(data+" ");
data=in.read();
}
try {
in.close();//ByteArrayInputStream的close()方法实际上不执行任何操作,那为什么要写呢?
还有in对象应该没有销毁吧,个人认为应该加上 in=null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
package day1;import java.io.ByteArrayInputStream;
import java.io.IOException;public class ByteArrayTester { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] buff=new byte[]{2,15,67,-1,-9,9};
ByteArrayInputStream in=new ByteArrayInputStream(buff,1,4);
int data=in.read();
while(data!=-1){
System.out.println(data+" ");
data=in.read();
}
try {
in.close();//ByteArrayInputStream的close()方法实际上不执行任何操作,那为什么要写呢?
还有in对象应该没有销毁吧,个人认为应该加上 in=null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
解决方案 »
- 如果独立窗口
- 大家来帮帮我
- float xx=rs.getFlloat("xx")出错,请高手指点
- 能否改变此程序让他避免出现NullPointerException
- applet与数据库的交互,设置java.policy的问题
- 如何创建一个文件可以覆盖原来的文件,如果它存在?
- 数据连接高分啊!!!
- 在 native 方法GetMyMethod(String str, MyClass cls)中,如何在实现这个native方法的VC程序DLL中使用这个MyClass ?
- 呵呵。剛學!問個問題
- 动态生成的孔件怎么添加响应事件,并在里面写代码?
- java实现RichTextBox求助
- Swing自定义UI的问题
package day1;import java.io.ByteArrayInputStream;
import java.io.IOException;public class ByteArrayTester { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] buff=new byte[]{2,15,67,-1,-9,9};
ByteArrayInputStream in=new ByteArrayInputStream(buff,1,4);
int data=in.read();
while(data!=-1){
System.out.println(data+" ");
data=in.read();
}
try {
in.close();//ByteArrayInputStream的close()方法实际上不执行任何操作,那为什么要写呢?
还有in对象应该没有销毁吧,个人认为应该加上 in=null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
上面的没标出来..再来下
流用完了要关闭啊
关闭了虚拟机才知道其已经没有价值了
空闲的时候垃圾回收机制会回收的
还有in对象应该没有销毁吧,个人认为应该加上 in=null;
in=null;是显示的告诉虚拟机它已经没有价值了 可以回收 但什么时候回收是虚拟机决定的
不用显示的写出来 在其没有价值的时候 虚拟机也会回收它的 其实in=null;这样的代码意义不大
你可以自己+上判断来调节
不过我想问下lz
你传输完毕以后不关闭,想继续传么?那为什么还要判断?
如果不传了,为什么不关闭?等他报溢出的异常么?
================
不懂你的想法
/**
* Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
* this class can be called after the stream has been closed without
* generating an <tt>IOException</tt>.
* <p>
*/
public void close() throws IOException {
}明确告诉你了,关了也对 ByteArrayInputStream 没任何影响,因为方法体是空的,当然调用 close 方法会耗用一星半点
的完全可忽略的计算资源。那为什么还要实现此方法呢?多次一举?切听下面详解:
我们通常使用的输入流一端是 Java 应用程序,而另一端通常是硬件设备或操作系统提供的本地资源(不在JVM管理范围内的系统资源),例如 FileInputStream 的另一端是OS的文件系统,Socket.getInputStream() 得到的输入流的另一端是网卡驱动和物理网络接口卡(NIC),当使用完此类输入流时,一般要显式地调用其 close 方法来释放本地资源。而 ByteArrayInputStream 的另一端是 JVM 管辖下的 byte[],当然不需要 close 方法。之所以 ByteArrayInputStream 提供了一个什么也不做的方法,一方面是因为其超类 InputStream 有此方法(方法体一样一行代码没有),所以即便 ByteArrayInputStream 不提供 close 方法也会从其超类继承下来 close 方法,而之所以要覆盖继承的方法而又不改变其功能是因为要在 ByteArrayInputStream 的文档注释中要明确地告诉你“Closing a ByteArrayInputStream has no effect. The methods inthis class can be called after the stream has been closed without generating an IOException.” 。