2012-07-23 52 views
0

我想創建zip文件。文件將包含導出的PreferencesSerializable對象。但是,當我嘗試替換對象在zip存檔保存首選項disapear。如何解決這個問題?替換ZipOutputStream中的文件

import java.io.BufferedOutputStream; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.util.prefs.BackingStoreException; 
import java.util.prefs.InvalidPreferencesFormatException; 
import java.util.prefs.Preferences; 
import java.util.zip.Deflater; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 
import java.util.zip.ZipOutputStream; 

public class ToZip { 

    /** 
    * @param args 
    */ 
    static Preferences exportPrefs = Preferences 
      .userNodeForPackage(ToZip.class); 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     exportPrefs.put("Val", "MyVal"); 

     writeFile(); 
     readFile(); 
     System.out.println("Value:" + exportPrefs.get("Val", "")); 

     // And Try replace object 
     writeObject(); 
     readObject(); 
    } 

    private static void writeFile() { 

     String str = "ABCD"; 

     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream("/home/user/profile.prof"); 
      BufferedOutputStream boS = new BufferedOutputStream(fos); 
      ZipOutputStream zoS = new ZipOutputStream(boS); 
      ObjectOutputStream ooS = null; 

      zoS.setMethod(ZipOutputStream.DEFLATED); 
      zoS.setLevel(Deflater.BEST_COMPRESSION); 

      zoS.putNextEntry(new ZipEntry("Object")); 

      ooS = new ObjectOutputStream(zoS); 
      ooS.writeObject(str); 

      zoS.putNextEntry(new ZipEntry("Profile")); 
      exportPrefs.exportSubtree(zoS); 

      try { 
       ooS.close(); 
      } catch (NullPointerException ex) { 
      } 

      zoS.close(); 
      fos.close(); 
      boS.close(); 
     } catch (FileNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (BackingStoreException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

    } 

    private static void readFile() { 

     FileInputStream fiN; 
     try { 
      fiN = new FileInputStream("/home/user/profile.prof"); 
      ZipInputStream ziS = new ZipInputStream(fiN); 

      ziS.getNextEntry(); 

      ObjectInputStream oiS = new ObjectInputStream(ziS); 
      System.out.println("Read String " + oiS.readObject()); 

      ziS.getNextEntry(); 

      exportPrefs.importPreferences(ziS); 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvalidPreferencesFormatException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    private static void writeObject() {// replace serialize object 

     String str2 = "XYZ"; 

     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream("/home/user/profile.prof"); 
      BufferedOutputStream boS = new BufferedOutputStream(fos); 
      ZipOutputStream zoS = new ZipOutputStream(boS); 
      ObjectOutputStream ooS = null; 

      zoS.setMethod(ZipOutputStream.DEFLATED); 
      zoS.setLevel(Deflater.BEST_COMPRESSION); 

      zoS.putNextEntry(new ZipEntry("Object")); 

      ooS = new ObjectOutputStream(zoS); 
      ooS.writeObject(str2); 

      try { 
       ooS.close(); 
      } catch (NullPointerException ex) { 
      } 

      zoS.close(); 
      fos.close(); 
      boS.close(); 
     } catch (FileNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

    } 

    private static void readObject() { 

     FileInputStream fiN; 
     try { 
      fiN = new FileInputStream("/home/user/profile.prof"); 
      ZipInputStream ziS = new ZipInputStream(fiN); 
      ziS.getNextEntry(); 

      ObjectInputStream oiS = new ObjectInputStream(ziS); 
      System.out.println("Read String " + oiS.readObject()); 

      oiS.close(); 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 

回答

1

你似乎並不被導出偏好您writeObject()也不在你readObject()方法閱讀。

writeObject(),你錯過:

 exportPrefs.exportSubtree(zoS); 

而你不讀他們在你readObject()

 exportPrefs.importPreferences(ziS); 
+0

你不明白我的。我只想更改「對象」,並且不要更改zip存檔中的其他文件。 – user902691 2012-07-23 20:13:35

+0

@ user902691您正在調用'writeFile()'將一個對象寫入一個zip文件,然後調用首選項,然後調用'readFile()'從zip文件讀取一個對象,然後讀取首選項。然後你調用'writeObject()'寫入(通過覆蓋現有文件)另一個對象到一個zip文件,而不是首選項。這就是爲什麼你錯過了偏好。 – 2012-07-23 20:17:03

+0

好的。但如何將文件附加到歸檔不會損失其他文件到zip? – user902691 2012-07-23 20:48:56