समस्या को उजागर करने के लिए कोड जॉर्ज Hategan लिए बहुत धन्यवाद। मैंने थोड़ी देर के लिए भी इसकी जांच की। फिर, यह मुझे मारा।आप डेटा लिखने के लिए एक उप-वर्गीकृत ObjectOutputStreamwriteStreamHeader() है, जिसकी ओवरराइड साथ विधि का उपयोग कर रहे हैं, तो आप है, जिसकी ओवरराइड के साथ समानांतर उप वर्गीकृत ObjectInputStream का उपयोग करना चाहिए डेटा पढ़ने के लिए readStreamHeader() विधि। बेशक, हम लेखन और पढ़ने की वस्तुओं के विभिन्न कार्यान्वयन के बीच ज़िग-ज़ैग कर सकते हैं, लेकिन जब तक हम लिखने/पढ़ने की प्रक्रिया में उप-वर्गों के संबंधित जोड़े का उपयोग करते हैं - हम (उम्मीदपूर्वक) ठीक होंगे। टॉम।
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
public class SerializationDemo {
public static void main(String[] args) throws Exception {
File storageFile = new File("test.ser");
storageFile.delete();
write(storageFile, getO1());
write(storageFile, getO2());
write(storageFile, getO2());
FileInputStream fis = new FileInputStream(storageFile);
read(fis, getO1());
read(fis, getO2());
read(fis, getO2());
fis.close();
}
private static void write(File storageFile, Map<String, String> o)
throws IOException {
ObjectOutputStream oos = getOOS(storageFile);
oos.writeObject(o);
oos.flush();
oos.close();
}
private static void read(FileInputStream fis, Map<String, String> expected)
throws ClassNotFoundException, IOException {
Object actual = getOIS(fis).readObject();
assertEquals(expected, actual);
System.out.println("read serialized " + actual);
}
private static void assertEquals(Object o1, Object o2) {
if (!o1.equals(o2)) {
throw new AssertionError("\n expected: " + o1 + "\n actual: " + o2);
}
}
private static Map<String, String> getO1() {
Map<String, String> nvps = new HashMap<String, String>();
nvps.put("timestamp", "1326382770000");
nvps.put("length", "246");
return nvps;
}
private static Map<String, String> getO2() {
Map<String, String> nvps = new HashMap<String, String>();
nvps.put("timestamp", "0");
nvps.put("length", "0");
return nvps;
}
private static ObjectOutputStream getOOS(File storageFile)
throws IOException {
if (storageFile.exists()) {
// this is a workaround so that we can append objects to an existing file
return new AppendableObjectOutputStream(new FileOutputStream(storageFile, true));
} else {
return new ObjectOutputStream(new FileOutputStream(storageFile));
}
}
private static ObjectInputStream getOIS(FileInputStream fis)
throws IOException {
long pos = fis.getChannel().position();
return pos == 0 ? new ObjectInputStream(fis) :
new AppendableObjectInputStream(fis);
}
private static class AppendableObjectOutputStream extends
ObjectOutputStream {
public AppendableObjectOutputStream(OutputStream out)
throws IOException {
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {
// do not write a header
}
}
private static class AppendableObjectInputStream extends ObjectInputStream {
public AppendableObjectInputStream(InputStream in) throws IOException {
super(in);
}
@Override
protected void readStreamHeader() throws IOException {
// do not read a header
}
}
}
स्रोत
2012-03-15 23:38:31
क्या आपके पास कोई प्रश्न है? – danben
क्या आपके लिए दृढ़ता के लिए डेटाबेस का उपयोग करना संभव है? – Asaph
@ डैनबेन: हाँ, सवाल यह है कि यदि वस्तुओं को किसी फ़ाइल में जोड़ा जा सकता है। @ एसाफ: मैं ऐसा कर सकता था, लेकिन मैं जानना चाहता हूं कि वस्तुओं के लिए परिशिष्ट नहीं है या नहीं। सूखी अगर मेरे प्रश्न ने इसे स्पष्ट नहीं किया। – starcorn