코드 노출 문제에 대해 George Hategan에게 감사드립니다.. 나는 잠시 그것을 조사했다. 그런 다음, 그것은 나를 때렸다.데이터를 작성하는 하위 분류 writeStreamHeader()의 재정의와 ObjectOutputStream에 방법을 사용하는 경우, 당신은 의 재정의와 병렬 하위 분류 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
}
}
}
질문이 있으십니까? – danben
지속성을 위해 데이터베이스를 사용할 수 있습니까? – Asaph
@danben : 네, 문제는 파일에 객체를 추가 할 수 있는지 여부입니다. @Asaph : 그렇게 할 수는 있지만 추가가 객체에 대해 아니오인지 알고 싶습니다. 제 질문으로 그 사실을 분명히 알 수없는 경우 Sry. – starcorn