try {
FileInputStream fs = new FileInputStream("e://aa.txt");
FileOutputStream fo = new FileOutputStream("e://b.txt");
byte b[] = new byte[5];
b[0] = (char) 'x';
while ((fs.read(b, 0, 512)) != -1) {
fo.write(b);
System.out.println("shoudao");
}
fs.close();
fo.close();
} catch (IOException e) {
System.out.println(e);
} catch (Exception e1) {
System.out.println(e1);
}
错误提示:
java.lang.IndexOutOfBoundsException

解决方案 »

  1.   

    byte b[] = new byte[5];改成: byte b[] = new byte[512];
    byte数组空间不够
      

  2.   

    数组越界
    fs.read(b, 0, 512)
    你数组b才5个字节,怎么从0读到512个字节啊
      

  3.   

    try {
    FileInputStream fs = new FileInputStream("e://aa.txt");
    FileOutputStream fo = new FileOutputStream("e://b.txt");
    byte b[] = new byte[512];
    b[0] = (char) 'x';
    while ((fs.read(b, 0, 512)) != -1) {
    fo.write(b);
    System.out.println("shoudao");
    }
    fs.close();
    fo.close();
    } catch (IOException e) {
    System.out.println(e);
    } catch (Exception e1) {
    System.out.println(e1);
    }