序列化对象的文件扩展名 [英] File extension for a Serialized object

查看:95
本文介绍了序列化对象的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Serializable对象保存到磁盘时,哪种文件扩展名最合适?

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);
}

推荐答案

.ser"是文件后缀的合理选择-

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

Java序列化的目标文件只能(通过常规方式)由在其类路径上具有相关类的Java应用程序读取.大小适合所有人的".ser"后缀没有提供有关这些类可能是什么以及应用程序启动框架应该在哪里找到它们的线索.因此,您将无法设置Windows样式的文件关联来提供双击应用程序的启动.

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.

有没有一种方法可以查看或编辑ser文件?

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

可能,但是难度很大,并且仅在特定条件下才能实现.

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

文件的内容高度依赖于序列化的类.现在可以确定该类的名称,以及(在某些情况下)序列化字段的名称和类型.但是,如果该类使用自定义序列化/外部化,则表示形式将是二进制数据的不透明斑点,文件中没有任何关于如何对其进行解码的线索.不透明的Blob问题相当普遍,因为Java SE类库中的许多重要类都使用自定义序列化...以提高效率...许多应用程序类也是如此.

The contents the file is highly dependent o the class that was serialized. Now it is possible to determine what the name of that class is, 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.

另一种可能的方法是为.ser文件中提到的所有类找到.class文件,反序列化为对象,并使用反射来访问反序列化对象的字段.您甚至可以调整它们并重新序列化它们.但是,如果您没有所有.class文件的正确版本,则这不是入门.

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.

这篇关于序列化对象的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆