我想创建 zip 文件。文件将包含导出Preferences
和Serializable
对象。但是当我尝试替换 zip 存档中的对象时,保存的首选项消失了。如何解决这个问题?
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();
}
}
}