那么如何确定InputStream的大小而开辟相应大小的byte[]?这个byte[]的大小可以根据文件的大小来确定。也可以根据每次读进的东西的大小来确定。比如可以这样byte[] b=new byte[500];
int s=in.read(b,0,500);
int i=0;
while(s!=-1){
System.out.println(i+++":"+s);
s=in.read(b,0,500);
}
System.out.println("这时候的返回结果:"+s);
int s=in.read(b,0,500);
int i=0;
while(s!=-1){
System.out.println(i+++":"+s);
s=in.read(b,0,500);
}
System.out.println("这时候的返回结果:"+s);
while ((c = is.read()) != -1) {
//do something...
//循环似乎挂在这里了
}
if (c == -1) {
System.out.println("client's c = -1 now..."); //没有被打印出来
}
byte[]的大小可以根据文件的大小来确定,如何实现呢?
比如要传输某个文件,在接收这方需要开辟一个相同大小的byte[]来接收,文件的大小又如何取得,又如何判断文件已经传输结束呢?难道只能在发送方写入某个结束符吗,比如"/r/n"用来表示结束吗?