File extension for a Serialized object
Asked Answered
L

1

22

What file extension would be most suitable when saving a Serializable object to disk?

FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
    fos = new FileOutputStream(filename);
    out = new ObjectOutputStream(fos);
    out.writeObject(mySerializableObject);
} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    IOUtils.closeQuietly(out);
}
Lenten answered 3/5, 2012 at 14:13 Comment(1)
i'm thinking .ser at the moment but not 100%Lenten
T
41

".ser" is a reasonable choice for the file suffix - http://www.file-extensions.org/ser-file-extension

However, you could argue that it make little difference what suffix you use ... provided that is doesn't clash other commonly used application suffixes.

A Java serialized object file can only be read (in the normal way) by a Java application that has the relevant classes on its classpath. A one-size-fits-all ".ser" suffix provides no clues as to what those classes might be and where an application launch framework should find them. So you won't be able to set up a Windows-style file association to provide double-click application launching.


Is there a way i can view or edit a ".ser" file ??

Possibly, but with great difficulty, and only under certain conditions.

The contents the file will be highly dependent on the class that was serialized. Now it is possible to determine the name of that class, and (in some cases) the names and types of the serialized fields. However if the class uses custom serialization / externalization, the representation will be an opaque blob of binary data with no clues in the file as to how to decode it. The opaque blob problem is fairly common because many important classes in the Java SE class library use custom serialization ... for efficiency ... and so do many application classes.

The other possible approach is to find the .class files for all of the classes mentioned in the .ser file, deserialize to objects, and use reflection to access the fields of the deserialised objects. You could even tweak them and reserialize them. However, if you don't have the right versions of all of the .class files, this is a non-starter.

Finally, editing a ".ser" file using a binary editor is technically possible, but would be dangerous. The serialization format is not designed to support this.

Tropicalize answered 3/5, 2012 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.