Java – file extension for serialized objects

What is the most appropriate file extension when saving serializable objects 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);
}

Solution

". ser" is the file suffix- http://www.file-extensions.org/ser-file-extension Reasonable choice of

However, you can think that it makes little difference what suffix it uses, as long as it does not conflict with other commonly used application suffixes

Java serialized object files can only be read (in the normal way) by Java applications with related classes on their classpath A one size fits all ". Ser" suffix does not provide clues to what these classes may be and where the application startup framework should find them Therefore, you will not be able to set up a Windows - style file association to provide double - click application startup

Possible, but difficult, only under certain conditions

The contents of the file are highly dependent on the serialized class You can now determine the name of the class and, in some cases, the name and type of the serialized field However, if the class uses custom serialization / externalization, the representation will be an opaque binary data block, and there is no clue about how to decode in the file Opaque blob problems are quite common because many important classes in the Java se class library use custom serialization... For efficiency... So are many application classes

Another possible way is to find Of all classes mentioned in the ser file Class file, deserialize the object, and use reflection to access the fields of the deserialized object You can even adjust them and reserve them But if you don't have everything The correct version of the class file, which is not the initiator

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>