このトピックで私が見つけた最高の記事は次のとおりです。http:
//codify.flansite.com/2009/11/java-serialization-appending-objects-to-an-existing-file/
ObjectOutputStreamをオーバーライドする「ソリューション」は単に間違っています。それによって引き起こされたバグの調査を終えたところです(貴重な2日間を無駄にしました)。シリアル化されたファイルが破損することがあるだけでなく、例外をスローせずに読み取り、最終的にガベージデータ(混合フィールド)を提供することさえできました。信じられない人のために、私は問題を明らかにするいくつかのコードを添付しています:
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws Exception {
File storageFile = new File("test");
storageFile.delete();
write(storageFile, getO1());
write(storageFile, getO2());
write(storageFile, getO2());
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(storageFile));
read(ois, getO1());
read(ois, getO2());
read(ois, getO2());
}
private static void write(File storageFile, Map<String, String> o) throws IOException {
ObjectOutputStream oos = getOOS(storageFile);
oos.writeObject(o);
oos.close();
}
private static void read(ObjectInputStream ois, Map<String, String> expected) throws ClassNotFoundException, IOException {
Object actual = ois.readObject();
assertEquals(expected, 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 class AppendableObjectOutputStream extends ObjectOutputStream {
public AppendableObjectOutputStream(OutputStream out) throws IOException {
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {
// do not write a header
}
}
}
その記事で述べられているように、次のいずれかの解決策を使用できます。
解決策#1:単一のストリームで複数のファイルを偽造する
..。
「トランザクション」をByteArrayOutputStreamに書き込み、次にこのByteArrayOutputStreamの長さと内容をDataOutputStreamを介してファイルに書き込みます。
解決策2:再度開いてスキップする
別の解決策は、以下を使用してファイル位置を保存することです。
long pos = fis.getChannel().position();
次のトランザクションを読み取る前に、ファイルを閉じてファイルを再度開き、この位置にスキップします。