黑马程序员技术交流社区
标题:
数组越界~!!!
[打印本页]
作者:
张志明
时间:
2014-2-7 22:57
标题:
数组越界~!!!
byte[]
buffer=new byte[1024];
private DataInputStream myDataInputStream = new DataInputStream(new BufferedInputStream(mySocket.getInputStream()));
private FileOutputStream myFileOutputStream = new FileOutputStream(new File("C\\:test.png"));
public void getFile(){
int count=0;int all=0;do{count=myDataInputStream.read(buffer,0,buffer.length);
myFileOutputStream.write(buffer,0,count);
all+=count;//记录一共接收了多少文件
}
while(count>-1);
}
复制代码
最后all的值为9999,并且getFile()报出了数组越界异常。
为什么呢~!!???
努力进黑马~!!!
作者:
杨道红
时间:
2014-2-8 09:59
byte[]
buffer=new byte[1024];
private DataInputStream myDataInputStream = new DataInputStream(new BufferedInputStream(mySocket.getInputStream()));
private FileOutputStream myFileOutputStream = new FileOutputStream(new File("C\\:test.png"));
public void getFile(){
int count=0;int all=0;
/*do{count=myDataInputStream.read(buffer,0,buffer.length);
myFileOutputStream.write(buffer,0,count);
all+=count;//记录一共接收了多少文件
}
while(count>-1);
*/
while((count=myDataInputStream.read(buffer,0,buffer.length))!=-1){
myFileOutputStream.write(buffer,0,count);
all+=count;//记录一共接收了多少文件
}
}
复制代码
你用do while循环中 do肯定会执行一次。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2