隨筆-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 閱讀(1689) 評論(2)  編輯  收藏 所屬分類: JAVA/J2EE

          評論:
          # re: ObjectOutputStream的多次讀寫問題[主要是writeStreamHeader] 2008-12-11 12:19 | kpzxc
          riruyg6  回復  更多評論
            
          # re: ObjectOutputStream的多次讀寫問題[主要是writeStreamHeader] 2009-11-17 11:41 | yunghe
          非常感謝,本文解決了我的大問題。  回復  更多評論
            
          主站蜘蛛池模板: 文山县| 汝州市| 疏勒县| 仲巴县| 张家界市| 榆树市| 遵义县| 荆州市| 青铜峡市| 锡林浩特市| 花莲县| 莱西市| 苍南县| 华蓥市| 建始县| 遂平县| 南汇区| 民权县| 大同县| 房产| 扶沟县| 永丰县| 海伦市| 肃北| 宁阳县| 刚察县| 福清市| 花莲市| 武夷山市| 六枝特区| 安庆市| 萨嘎县| 紫云| 绍兴县| 安徽省| 广水市| 南宁市| 嘉义市| 永平县| 高邑县| 玛纳斯县|