views:

70

answers:

3

I'm on my way in developing a desktop application using netbeans(Java Dextop Application) and I need to implement my own file format which is specific to that application only. I'm quite uncertain as to how should I go about first.What code should I use so that my java application read that file and open it in a way as I want it to be.

+2  A: 

If it's character data, use Reader/Writer. If it's binary data, use InputStream/OutputStream. That's it. They are available in several flavors, like BufferdReader which eases reading a text file line by line and so on.

They're part of the Java IO API. Start learning it here: Java IO tutorial.

By the way, Java at its own really doesn't care about the file extension or format. It's the code logic which you need to write to handle each character or byte of the file according to some file format specification (which you in turn have to writeup first if you'd like to invent one yourself).

BalusC
+1  A: 

I am not sure this directly addresses your question, but since you mentioned a custom file format, it is worth noting that applications launched using Java Web Start can declare a file association. If the user double clicks one of those file types, the file name will be passed to the main(String[]) of the app.

This ability is used in the File Service demo. of the JNLP API - available at my site.

As to the exact format of the file & the best ways to load and save it, there are a large number of possibilities that can be narrowed down with more details of the information it contains.

Andrew Thompson
A: 

Choosing a new/existing file extension does not affect your application (or in any case anyone's). It is upto the programmer what files he wants his app to read.

For example, you may consider you can't read a pdf or doc directly as a text file....but that is not because they are written/ stored differently, but because they have headers or characters which your app does not understand. So we might use a plugin or extension which understands those added headers ( or rather the grammar of the pdf /doc file) removes them & lets our app know what text (or anything else) it contains.

So if you wish to incorporate your own extension, & specifically want no other application to be able to read it, just write the text in a way that only your program is able to understand. Though writing a file in binary pretty much ensures that your file is not read directly just by user opening a file, but it is however still possible to read from it, if it is merely collection of raw characters.

If you ask code for hiding a data, I'd say there are plenty of algorithms you might use, which usually get tagged as encryptions cause you are basically trying to lock/hide your stuff. So if you do not really care for the big hulla-bulla, simply trying to keep a file from being directly read & successful attempts to read the file does not cause any harm to your application, write it in binary.

loxxy
Nice collection of words there, but where's the relevance?
cHao
I believe he said he is on the way to create an application, probably giving an impression that he is beginning to start things. I guess mine was an attempt (probably failed one) to let him understand why extensions matter or not.
loxxy
The custom file format apparently has already been decided on as the way of doing stuff. Custom file formats should usually have custom extensions, IMO. There wasn't any mention of contents only being *accessible* by that application; rather, the implication was that the data (being in a custom format) would be *understood* only by the app -- and probably, that the format's extension needs to be registered somehow so that the OS will know to pass requests for those files off to the app.
cHao
Oh....seriously, I never thought that way. I had actually considered something different, in reference to people trying to keep fancy extensions for their new apps. I guess I should have read more carefully before jumping in :)
loxxy