在下面的例子中关闭输出流后仍然可以从输出流中得到字节数组,那么关闭输出流的的作用是什么,close()完成了那些工作,谢谢!
import java.io.*;
public class PrintStreamTester{
private static void readBuff(byte[] buff)throws IOException{
ByteArrayInputStream in=new ByteArrayInputStream(buff);
int data;
while((data=in.read())!=-1)
System.out.print(data+" ");
System.out.println();
in.close();
} public static void main(String agrs[])throws IOException{
//通过DataOutputStream写字符串“好”
out=new ByteArrayOutputStream();
DataOutputStream ds=new DataOutputStream(out);
ds.writeUTF("好");
ds.close(); buff=out.toByteArray(); //获得字节数组
System.out.println("采用适用于Java语言的UTF-8字符编码:");
readBuff(buff);
}
}
import java.io.*;
public class PrintStreamTester{
private static void readBuff(byte[] buff)throws IOException{
ByteArrayInputStream in=new ByteArrayInputStream(buff);
int data;
while((data=in.read())!=-1)
System.out.print(data+" ");
System.out.println();
in.close();
} public static void main(String agrs[])throws IOException{
//通过DataOutputStream写字符串“好”
out=new ByteArrayOutputStream();
DataOutputStream ds=new DataOutputStream(out);
ds.writeUTF("好");
ds.close(); buff=out.toByteArray(); //获得字节数组
System.out.println("采用适用于Java语言的UTF-8字符编码:");
readBuff(buff);
}
}
楼主【topcs】截止到2008-07-13 19:52:35的历史汇总数据(不包括此帖):
发帖的总数量:3 发帖的总分数:60 每贴平均分数:20
回帖的总数量:4 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:3 结贴的总分数:60
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:0.00 % 无满意结分率:0.00 %
敬礼!
你用一个真正的流看看,比如读取本地文件,或者互联网的页面。
/**
* Creates a newly allocated byte array. Its size is the current
* size of this output stream and the valid contents of the buffer
* have been copied into it.
*
* @return the current contents of this output stream, as a byte array.
* @see java.io.ByteArrayOutputStream#size()
*/
public synchronized byte toByteArray()[] {
return Arrays.copyOf(buf, count);
}它返回的是一个buf这个字节数组的复制品。
buf这个byte[]本来就是在内存里面的,
所以你关不关流无所谓。第一次知道当一个方法返回一个数组的时候可以这么写:public int getIntArray()[] {
return new int[10];
}
关闭 ResultSet 对象不会关闭 ResultSet 创建的 Blob、Clob 或 NClob 对象。Blob、Clob 或 NClob 对象至少在创建它们的事务期间持续有效,除非调用了其 free 方法。 当关闭 ResultSet 时,所有通过调用 getMetaData 方法创建的 ResultSetMetaData 实例都可以访问。 注:当生成 ResultSet 对象的 Statement 对象关闭、重新执行或用来从多个结果的序列获取下一个结果时,该 Statement 对象将自动关闭 ResultSet 对象。 在已关闭的 ResultSet 对象上调用 close 方法是无操作 (no-op)。
* Closing a <tt>ByteArrayOutputStream</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 {
}