日期:2014-05-20  浏览次数:20740 次

读文件 ... 不是很熟悉java,看看为什么出现错误?
Java code
File file = new File(FileName) ;           
java.io.FileReader fr=new java.io.FileReader(file); 
char[] cbuf=new char[1024];
Integer offset=Integer.valueOf(1);
Integer iRead;
iRead=fr.read(cbuf,offset,1024);


出现如下错误:
Exception in thread "main" java.lang.IndexOutOfBoundsException
  at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:131)


------解决方案--------------------
offset是不是不是应该从0开始?
------解决方案--------------------
看看StreamDecoder.java的131行
------解决方案--------------------
数组越界,数组下标应该从0开始,1023结束进行阅读,你这样读取的是第二个到第1025个数组元素,显然是越界了
------解决方案--------------------
看错了,1楼正解,从0开始

offset是cbuf的偏移量吧
read(cbuf,1,1023)
read(cbuf,0,1024)