zoukankan      html  css  js  c++  java
  • ObjectOutputStream 追加写入读取错误

     本篇博客灵感来自http://blog.csdn.net/chenssy/article/details/13170015

     问题描述、问题出现的原因、尝试解决办法,请参见鄙人上一编博客。

    上一编文章解决ObjectOutputStream 追加写入读取错误问题的方法是自定义了一个ObjectOutputStream子类,我觉得不如用匿名内部类实现方便,于是自我研究写出以下两种方案。个人更推崇方案二

    方案一:

     1 package packa;
     2 
     3 import java.io.*;
     4 
     5 class ObjectInputOutputStream2
     6 {
     7     public static void main(String[] args)throws Exception
     8     {
     9         writeObj();
    10         readObj();
    11     }
    12 
    13     private static void writeObj()throws Exception
    14     {
    15         final File f = new File("person.object");
    16         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.object", true))
    17         {
    18             protected  void writeStreamHeader()throws IOException
    19             {
    20                 if (!f.exists() || (f.exists() && 0 == f.length()))
    21                 {
    22                     super.writeStreamHeader();
    23                 }
    24             }
    25         };
    26 
    27         oos.writeObject(new Person("liu", 30, "kr"));
    28         oos.writeObject(new Person2("liu", "kr", "kr2"));
    29         oos.writeObject(new Person3(10, 30, 50));
    30 
    31         oos.close();
    32     }
    33 
    34     private static void readObj()throws Exception
    35     {
    36         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.object"));
    37 
    38         try
    39         {
    40             while(true)
    41             {
    42                 System.out.println(ois.readObject());    
    43             }
    44         }
    45         catch (Exception e)
    46         {
    47             System.out.println(e.toString());
    48         }
    49 
    50         ois.close();
    51     }
    52 }

    方案二:

     1 package packa;
     2 
     3 import java.io.*;
     4 
     5 class ObjectInputOutputStream
     6 {
     7     public static void main(String[] args)throws Exception
     8     {
     9         writeObj();
    10         readObj();
    11     }
    12 
    13     static ObjectOutputStream  getObjectOutputStreamInstance(final OutputStream os, final File f) throws IOException
    14     {
    15         return  new ObjectOutputStream(os)
    16         {
    17             protected  void writeStreamHeader()throws IOException
    18             {
    19                 if (!f.exists() || (f.exists() && 0 == f.length()))
    20                 {
    21                     super.writeStreamHeader();
    22                 }
    23             }
    24         };
    25     }
    26 
    27     private static void writeObj()throws Exception
    28     {
    29         ObjectOutputStream oos = getObjectOutputStreamInstance(new FileOutputStream("person.object", true), new File("person.object"));
    30         oos.writeObject(new Person("liu", 30, "kr"));
    31         oos.writeObject(new Person2("liu", "kr", "kr2"));
    32         oos.writeObject(new Person3(10, 30, 50));
    33 
    34         oos.close();
    35     }
    36 
    37     private static void readObj()throws Exception
    38     {
    39         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.object"));
    40 
    41         try
    42         {
    43             while(true)
    44             {
    45                 System.out.println(ois.readObject());    
    46             }
    47         }
    48         catch (Exception e)
    49         {
    50             System.out.println(e.toString());
    51         }
    52 
    53         ois.close();
    54     }
    55 }
     1 package packa;
     2 
     3 import java.io.*;
     4 
     5 class Person implements Serializable
     6 {
     7     public static final long serialVersionUID = 44L;
     8 
     9     String name;
    10     transient int age;
    11     static String country = "cn";
    12 
    13     Person(String name, int age, String country)
    14     {
    15         this.name = name;
    16         this.age = age;
    17         this.country = country;
    18     }
    19 
    20     public String toString()
    21     {
    22         return name + " " + age + " " + country;
    23     }
    24 
    25 }
    26 class Person2 implements Serializable
    27 {
    28     public static final long serialVersionUID = 45L;
    29 
    30     String name;
    31     String country;
    32     String country2;
    33 
    34     Person2(String name, String country, String country2)
    35     {
    36         this.name = name;
    37         this.country = country;
    38         this.country2 = country2;
    39     }
    40 
    41     public String toString()
    42     {
    43         return name + " " + country + " " + country2;
    44     }
    45 
    46 }
    47 class Person3 implements Serializable
    48 {
    49     public static final long serialVersionUID = 46L;
    50 
    51     int age;
    52     int age2;
    53     int age3;
    54 
    55     Person3(int age, int age2, int age3)
    56     {
    57         this.age = age;
    58         this.age2 = age2;
    59         this.age3 = age3;
    60     }
    61 
    62     public String toString()
    63     {
    64         return age + " " + age2 + " " + age3;
    65     }
    66 
    67 }
  • 相关阅读:
    EF中读取随机数据的问题
    【坐在马桶上看算法】算法3:最常用的排序——快速排序
    C#递归算法
    机器学习策略(二)---误差分析、训练集与开发测试集不相配怎么办、迁移学习/多任务学习、端到端深度学习
    机器学习策略(一)---正交化、评估指标、优化指标、训练与测试集数据集大小、可避免误差
    改善深层神经网络的优化算法:mini-batch梯度下降、指数加权平均、动量梯度下降、RMSprop、Adam优化、学习率衰减
    具有单隐藏层的二分类神经网络
    神经网络前向后向传播(理论与实战)
    梯度消失与梯度爆炸---如何选择随机初始权重
    正则化输入
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/6115965.html
Copyright © 2011-2022 走看看