Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at propertiesDemo.myLoad(haha.java:21)
at propertiesDemo.main(haha.java:9)
我查了好几遍,实在找不出错误来,大伙帮我看看。
作者: 姚鑫 时间: 2012-4-2 23:54
这个程序不用改为 String line = null;也是可以运行的。line=bufr.readLine()就是一个赋值。
例如程序:
public class Test {
public static void main(String[] args) {
int i; // 不用定义为 int i = 0; 也是可以的
i = new Random().nextInt(10);
System.out.println(i);
}
}
这个程序的问题应该在于info.txt这个文件的格式有问题
假如info.txt中有空行,那么此时line="", str的lenth==0,str[0]、str[1]不存在,会报数组越界异常;
假如info.txt中有的行只有key,没有value,str的lenth==1,str[1]不存在,会报数组越界异常;
假如info.txt中有的行没有等号,line无法分割,也会报数组越界异常;
所以请检查info.txt的格式,注意末尾不要有回车换行
也可以将程序改为
public static void myLoad(Properties pr, InputStream in) throws IOException {
BufferedReader bufr = new BufferedReader(new InputStreamReader(in));