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

新手读文件出错
class   Haff_Node   implements   Serializable
{
char   ch;
String   hcode;
int   lchild;
int   rchild;
int   freq;
int   flag;
int   num;

}
我用这个方法存
//Haff_Tree是一个Haff_Node的数组
public   void   Save_Tree_to_Text()
{
try
{
ObjectOutputStream   out   =   new   ObjectOutputStream(new   FileOutputStream( "Haff_tree.dat "));

out.writeObject(Haff_Tree);
out.close();

System.out.println( "ok!save ");
}

catch(Exception   e)
{
e.printStackTrace();
}
}

但是用这个方法读的时候,什么都读不出来
public   void   Load_Text_to_Tree()
{
try
{
ObjectInputStream   in   =   new   ObjectInputStream(new   FileInputStream( "Haff_tree.dat "));
Haff_Node[]   Haff_Tree   =   (Haff_Node[])in.readObject();

in.close();

System.out.println( "ok!load ");
}
catch(Exception   e)
{
e.printStackTrace();
}
}


------解决方案--------------------
java.lang.ArrayIndexOutOfBoundsException: -1这的不是说越界么?
------解决方案--------------------
越界異常!
------解决方案--------------------
Haff_Node[] Haff_Tree = (Haff_Node[])in.readObject();

Haff_Node[]的大小是多少呢?你把读到的值给这个赋值,Haff_Node没有指定大小,当然越界了
------解决方案--------------------
Haff_Node[] Haff_Tree = new Haff_Node[1000];
然后再试试