标题:请教关于Object流的问题,Externalizable接口的应用测试
取消只看楼主
z511963074
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-4-27
结帖率:33.33%
 问题点数:0 回复次数:1 
请教关于Object流的问题,Externalizable接口的应用测试
import *;

public class TestObject {
    public static void main(String[] args){
        TestSer ts = new TestSer();
        TestExt te = new TestExt();
        try{
            FileOutputStream fos = new FileOutputStream("D:/JAVA/testStream/object.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(te);
            oos.flush();
            oos.close();
            
            FileInputStream fis = new FileInputStream("D:/JAVA/testStream/object.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            te = (TestExt)ois.readObject(); //**********************
            ois.close();
            
        }    catch(FileNotFoundException e){
            System.out.println("FileNotFoundException");
            e.printStackTrace();
            System.exit(-1);
            
        }    catch(IOException e){
            System.out.println("IOException");
            e.printStackTrace();
            System.exit(-1);
            
        }    catch(ClassNotFoundException e){
            System.out.println("ClassNotFoundException");
            e.printStackTrace();
            System.exit(-1);
        }
    }
}

//继承接口测试类
class TestExt implements Externalizable {
    String s = "hello";
   
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        s = (String)in.readObject();
    }
   
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(s);
    }
}

-----------------------------------------------------------------------------------------------------------
注意上面程序用注释***********标记的地方,那里抛出了
想了很久也不知道为什么。求大神们解答。。。
谢谢
搜索更多相关主题的帖子: static Object public import 
2013-05-13 16:46
z511963074
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-4-27
得分:0 
第一句忽略,我删除没有注释的时候删除漏了的!
2013-05-13 16:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-406981-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.711362 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved