java 如何创建自己的文件类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2445060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create own filetype?
提问by joseph
I want to create own filetype to save objects in my app. Basically, I urgently do not need new filetype, but it will be better.
我想创建自己的文件类型来保存我的应用程序中的对象。基本上,我迫切不需要新的文件类型,但它会更好。
I have class. For example Car. It has constructor (String name, String color, int length, Driver driver). When a car is created (its instance), how to save it like a file?
我有课。例如汽车。它具有构造函数(字符串名称、字符串颜色、整数长度、驱动程序驱动程序)。创建汽车(其实例)时,如何将其保存为文件?
回答by Pratik Deoghare
To save object to file you need serialization.
要将对象保存到文件,您需要序列化。
You can choose whatever file extension we like e.g. .car.
您可以选择我们喜欢的任何文件扩展名,例如.car.
回答by KTastrophy
I would save it as an XML file with a custom extension. I would them tell my application to read those files as XML. For me, that's the easiest way to ensure consistency. It can also help in the event that your application should need to communicate with another, perhaps on user request.
我会将它保存为带有自定义扩展名的 XML 文件。我会让他们告诉我的应用程序将这些文件读取为 XML。对我来说,这是确保一致性的最简单方法。如果您的应用程序需要与另一个应用程序进行通信(可能是应用户请求),它也可以提供帮助。
回答by zellio
Basically to 'create' your own file type you define a standard and use your own file extension. There aren't any large hoops to jump through so long as you are consistent.
基本上要“创建”您自己的文件类型,您需要定义一个标准并使用您自己的文件扩展名。只要您保持一致,就没有任何大的障碍可以跳过。
Though, what you mean by, save your objects, I am unsure.
虽然,你的意思是保存你的对象,我不确定。
Additionally look into the Object inputand outputstream methods. Finally look at thisfor Serialization of methods in java.
另外查看对象输入和输出流方法。最后看看thisfor Serialization of methods in java。

