隨筆-28  評論-51  文章-10  trackbacks-0
          Test.java   解決多次寫的時候,往往重復寫入header,導致讀出時,出現streamcorrput異常,所以這里要判斷是不是第一次寫文件,若是寫入頭部,否則不寫入
          這是通過重載
          writeStreamHeader來實現的


          1
           package file;
           2 import java.io.EOFException;
           3 import java.io.File;
           4 import java.io.FileInputStream;
           5 import java.io.FileNotFoundException;
           6 import java.io.FileOutputStream;
           7 import java.io.ObjectInputStream;
           8 import java.io.ObjectOutputStream;
           9 import java.io.Serializable;
          10 import java.io.StreamCorruptedException;
          11 import java.util.LinkedList;
          12 public class Test {
          13 
          14     private static LinkedList al = new LinkedList();
          15     public static void main(String[] args) {
          16         // TODO Auto-generated method stub
          17         System.out.println("welcome!!");
          18         try {
          19             File file = new File("input.txt");
          20             FileOutputStream out = new FileOutputStream(file,true);
          21             ObjectOutputStream oo=null;
          22             if(file.length()<1)
          23             {
          24                 oo = new ObjectOutputStream(out);
          25             }
          26             else
          27             {
          28                 oo = new MyObjectOutputStream(out);
          29             }
          30         
          31             int i = 0;
          32             Node n;
          33             while(i++<6)
          34                 {
          35                     n = new Node();
          36                     n.name = "kyle"+i;
          37                     oo.writeObject(n);
          38                 }
          39             oo.flush();
          40             oo.close();
          41             FileInputStream in = new FileInputStream("input.txt");
          42             ObjectInputStream oi = new ObjectInputStream(in);
          43              i = 0;
          44             
          45             while(true)
          46             {
          47                 System.out.println(i++);
          48                 al.add((Node)oi.readObject());
          49             }
          50 
          51         }
          52         catch(EOFException es)
          53         {
          54             return;
          55         }
          56         catch (Exception e) {
          57             // TODO Auto-generated catch block
          58             e.printStackTrace();
          59 
          60         }
          61     }
          62 }
          63 class Node implements Serializable
          64 {
          65     public Node()
          66     {
          67         name = name+ i++;
          68     }
          69     public  String name;
          70     private static int i = 0;
          71     public String toString()
          72     {
          73         return name;
          74     }
          75 }
          76 
          77 
          78 
          79 
          80 
          81 

          MyObjectOutputStream.java
           1 package test;
           2 
           3 import java.io.IOException;
           4 import java.io.ObjectOutputStream;
           5 import java.io.OutputStream;
           6 
           7 public class MyObjectOutputStream extends ObjectOutputStream {
           8 
           9     
          10     private static boolean flag = true;//判斷是否為第一次寫入文件
          11     private static boolean one = true;
          12 
          13     public MyObjectOutputStream() throws IOException, SecurityException {
          14         // TODO Auto-generated constructor stub
          24     }
          25 
          26     public MyObjectOutputStream(OutputStream out) throws IOException {
          27         super(out);
          28         // TODO Auto-generated constructor stub
          29     }
          30     protected void writeStreamHeader()
          31     throws IOException
          32     {
          33         return//不寫入頭部
          34     }
          35 
          36 }
          37 





          posted on 2008-04-03 00:43 fullfocus 閱讀(1688) 評論(2)  編輯  收藏 所屬分類: JAVA/J2EE

          評論:
          # re: ObjectOutputStream的多次讀寫問題[主要是writeStreamHeader] 2008-12-11 12:19 | kpzxc
          riruyg6  回復  更多評論
            
          # re: ObjectOutputStream的多次讀寫問題[主要是writeStreamHeader] 2009-11-17 11:41 | yunghe
          非常感謝,本文解決了我的大問題。  回復  更多評論
            
          主站蜘蛛池模板: 通化市| 安西县| 沙洋县| 和龙市| 瑞安市| 交城县| 灌云县| 苏尼特右旗| 邓州市| 焦作市| 樟树市| 黄梅县| 绥滨县| 安庆市| 宜阳县| 如皋市| 谢通门县| 天祝| 陇川县| 天等县| 桑植县| 永济市| 黄平县| 奉化市| 易门县| 油尖旺区| 阳朔县| 曲阳县| 甘南县| 会昌县| 格尔木市| 红安县| 息烽县| 阜城县| 衡山县| 英德市| 田东县| 华容县| 茌平县| 自贡市| 麻栗坡县|