我想要将一个对象保存到文件,而这个对象里面又含有Image类的对象,
用ObjectOutputStream的时候提示要Serializable接口才可以,于是我把
我自己的那个类给加上接口,然后运行又报错,提示Image类没序列化
接口,这下我就不知道怎么办了,Image类是系统的抽象类,怎么序列化啊?
系统的类可以修改的吗?请问要如何做?

解决方案 »

  1.   

    对象继承 Externalizable 接口,然后自己解决整个对象以及成员变量的序列化输入输出。
      

  2.   

    好吧看了下有点麻烦,我直径用ImageIcon对象得了,到时候再提取Image对象也可以。
    不过我发现一个严重的问题,ImageIcon读取50Kb的图片保存对象后,变为3倍大小。
    请问要如何实现压缩?
      

  3.   

    最好是直接保存原图片的信息(数据),不要存储时再压缩;有损压缩次数多了,越来越失真。ImageIO.write(image, "jpg", new File("savename.jpg")); 
      

  4.   

    自己写一个继承Image的子类  并实现序列化接口  在构造方法中直接调用父类构造方法  应该可行 
      

  5.   

    将Image这个变量前另上修饰符transient,然后实现以下两个方法.public class YouClass {  private Image image;
      
      //反序例化时的操作
      private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
        in.defaultReadObject();
       
        int imageLen = in.readInt();//读取图片长度
        ByteArrayOutputStream buff = new ByteArrayOutputStream();//buff
        int readLen = 0;
        byte[] readBuff = new byte[1024];
        while((len = in.read(readBuff) != -1) {
          buff.write(readBuff,0,readLen);
        }
      
        ByteArrayInputStream imageReadBuff = new ByteArrayInputStream(buff.toByteArray());
        image = ImageIO.read(imageReadBuff);
      }
      
      //序列化
      private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();    ByteArrayOutputStream buff = new ByteArrayOutputStream();
        //这里选用了JPG格式,其他格式也可以重点是将图片转换成某种格式的字节码
        //一般可以用"PNG","GIF","JPG"吧.
        ImageIO.write(image,"JPG",buff);
        
        byte[] images = buff.toByteArray();
        
        out.writeInt(image.length);
        out.write(images);
      }
    }
      

  6.   

    改正一下private Image image;应该是private transient Image image;