gpt4 book ai didi

java - 替换 ZipOutputStream 中的文件

转载 作者:行者123 更新时间:2023-12-01 15:15:55 26 4
gpt4 key购买 nike

我想创建 zip 文件。文件将包含导出的 PreferencesSerialized 对象。但是当我尝试替换 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();
}

}
}

最佳答案

您似乎没有在 writeObject() 中导出首选项,也没有在 readObject() 方法中读取它们。

writeObject() 中,您缺少:

        exportPrefs.exportSubtree(zoS);

并且您没有在 readObject() 中读取它们

        exportPrefs.importPreferences(ziS);

关于java - 替换 ZipOutputStream 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11619427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com