日期:2014-05-20  浏览次数:20598 次

关于java位元数组和ByteArrayInputStream的两个小问题
小弟初学Java,下面有两个小小的问题想要请教,望各位耐心为小弟指点一二!(注:【。。。】为有疑问的地方)
[code=Java][/code]
File file = new File(args[0]); 
BufferedInputStream bufferedInputStream = 
new BufferedInputStream( 
new FileInputStream(file)); 

// 将文件读入位元数组 
ByteArrayOutputStream arrayOutputStream = 
new ByteArrayOutputStream(); 
【byte[] bytes = new byte[1];】
while(bufferedInputStream.read(bytes) != -1) {
arrayOutputStream.write(bytes);
}
【arrayOutputStream.close();】 
bufferedInputStream.close(); 

// 显示位元数组内容 
【bytes = arrayOutputStream.toByteArray();】 
【for(byte b : bytes)】 {
System.out.print((char) b);
}
问题:一,【byte[] bytes = new byte[1];】明明是定义了只有一个位元的数组,为什么这里【for(byte b : bytes)】像是已经将文件的所有内容都输入了里面?
二,【arrayOutputStream.close();】 输出流关闭之后还能进行【bytes = arrayOutputStream.toByteArray();】 这样的操作吗?

------解决方案--------------------
Java code

while(bufferedInputStream.read(bytes) != -1) {
arrayOutputStream.write(bytes);
}