tags:

views:

95

answers:

3

Hello, 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?

A: 

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 input and output stream methods. Finally look at this for Serialization of methods in java.

Mimisbrunnr
I have class. For example Car. It has constructor (String name, String color, int length, Driver driver).When a car is created (its instance), I do not know how to save this instance like a file
joseph
Look at the classes I just added to my answer, I think they may be closer to what you are looking for.
Mimisbrunnr
+1  A: 

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.

KThompson
The advantage of XML (human readable formats in general) is that other apps can read your files as well. Just in case your program/service in no longer available.Hunt and Thomas discuss in The Pragmatic Programmer. The chapter of interest is called "The Power of Plain Text."
Matthew
Depending on the meaning of read. It might make it possible to view a xml text file that gives some amount of information. It might make it easier to right a consumer for that kind of xml file for other program, but if you create your own type of xml file other won't be able to just understand the info you want the file to store.
Roman A. Taycher
+1  A: 

To save object to file you need serialization.

You can choose whatever file extension we like e.g. .car.

Good serialization tutorial.

TheMachineCharmer
Serialization is not a very good option in a "real" application (as opposed to hello world or homework kind). Particularly if you don't write your own serializer/deserializer.
saugata
And why it is not good option for real app? Where I can find some tutorial for own serializer?
joseph
@saugata I don't think so. A lot of "real" applications use serialization.
TheMachineCharmer
@TheMachineCharmer I can't think of any situation where storing data in serialized form in filesystem is better than storing it in a DB. Can you please give an example.
saugata