【发布时间】:2014-10-10 18:46:49
【问题描述】:
我的程序将一个文件读入一个字节数组,然后尝试从该文件中提取一个 bmp 图像。问题是我遇到了越界错误。
{
public static void main( String[] args )
{
FileInputStream fileInputStream=null;
File file = new File("C:/thumbcache_32.db");
byte[] bFile = new byte[(int) file.length()];
System.out.println("Byte array size: " + file.length());
try {
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
//convert array of bytes into file
FileOutputStream fileOuputStream =
new FileOutputStream("C:/Users/zak/Desktop/thumb final/Carved_image.bmp");
fileOuputStream.write(bFile,1573278,1577427);
fileOuputStream.close();
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
}
}
}
文件加载到的字节数组的大小为“3145728”
我正在尝试将字节“1573278”复制到“1577427”。如您所见,这些字节在字节数组的范围内,所以我不确定为什么会出现此错误
程序运行时的输出
Byte array size: 3145728
java.lang.IndexOutOfBoundsException
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(Unknown Source)
at Byte_copy.main(Byte_copy.java:28)
【问题讨论】:
-
这是
write(data[], offset, len)(见docs.oracle.com/javase/7/docs/api/java/io/…)不是(data[], from, to) -
感谢您的帮助,我的代码现在可以正常工作了
标签: java indexoutofboundsexception