尝试用鼠标单击读取对象

时间:2016-02-26 19:18:11

标签: java object serialization

所以,我正在尝试阅读名为Kratisi的对象,但我总是遇到错误。我设法让它显示1个对象,但是当我添加一两个错误时,错误又回来了。错误是:

  

StreamCorruptedException:无效的类型代码AC

有人能帮助我吗?这是我的代码:

我的功能:

private ArrayList<Kratisi> readKratiseis() throws FileNotFoundException,IOException,ClassNotFoundException
{
    in=new ObjectInputStream(new FileInputStream(new File("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\kratiseis.txt")));
    ArrayList<Kratisi> tmp_list = new ArrayList<Kratisi>();
    Kratisi tmp_kratisi =null ;
    while(true)
    {
        try
        {
            tmp_kratisi = (Kratisi)in.readObject();
                if (tmp_kratisi!=null)
                {
                tmp_list.add(tmp_kratisi);
                }
        }catch(EOFException eof){return tmp_list;}
    }
}
private void closeFile() throws IOException
{
    in.close();
}

从班上打电话的方式:

try {

        list_kratiseis=readKratiseis();
        closeFile();
    } catch (IOException ex) {
        Logger.getLogger(AnazitisiInterface.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(AnazitisiInterface.class.getName()).log(Level.SEVERE, null, ex);
    }

注意:我从类的构造函数内部调用函数,ObjectReader在构造函数之前和之前声明。 另外,我不想写和读整个ArrayList,我想逐个读写Object。

谢谢。

0 个答案:

没有答案