Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

".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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...