public class Test {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("2.jpg");
FileOutputStream fos=new FileOutputStream("4.jpg");
int b;
byte[] bs=new byte[1024];
while((b=fis.read(bs))!=-1){
fos.write(bs,0,b);
}
fis.close();
fos.close();
}
}
我想问的是当没有读完1024字节,只读到100个的时候b应该返回的  什么?

解决方案 »

  1.   

    返回1024   因为你每读一次在bs中新的字节就会覆盖先前的 这一百个字节只覆盖啦上一次你读取的1024个字节
    中的一百个,但是bs的长度还是1024;
      

  2.   

    the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached. 
      

  3.   

    结果是100,你只需要在循环里面加上一个System.out.println(b);语句就可以了,
    到结尾的时候,打印出来的b就是你想知道的那种情况的数值。这是我修改后的测试程序
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;public class TT {
    public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("aa.txt");
    FileOutputStream fos = new FileOutputStream("bb.txt");
    int b;
    byte[] bs = new byte[1024];
    while ((b = fis.read(bs)) != -1) {
    System.out.println(b);
    fos.write(bs, 0, b);
    }
    fis.close();
    fos.close();
    }
    }
    这里的aa.txt文件最好小一点。