Gabriel Genellina:
[...]
>Maybe a binary format is more efficient, but I don't know which could
>be the best way, nor how to implement it.
There are DataInputStream and DataOutputStream. Both have read and
write method for the primitive types of Java and Strings. Byte order
is big endian, valid intervals for the primitives types are defined in
the Java specs (e.g. char from 0 to 65535), the format of String
serialization is described in the API docs of read/writeUTF.
So if an element would be like the data you described above, an
element class could be:
class Element {
String s;
int i;
float f;
String s2;
}
And reading and writing could work like that:
Element read(DataInputStream in) throws IOException {
Element elem = new Element();
elem.s = in.readUTF();
elem.i = in.readInt();
elem.f = in.readFloat();
elem.s2 = in.readUTF();
return elem;
}
void write(DataOutputStream out, Element elem) throws IOException {
out.writeUTF(elem.s);
out.writeInt(elem.i);
out.writeFloat(elem.f);
out.writeUTF(elem.s2);
}
There is no single best way of doing persistent storage. Personally
I'd work with databases whenever it's feasible. I don't like self-made
binary formats like the above very much. You can't change things
easily, at least not if you have to convert existing data from binary
format A to B. Other people will have to study your format and write
and maintain dedicated code.
However, the format is more efficient (less space and faster to parse)
than ASCII text.
Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips:
http://jiu.sourceforge.net/javatips.html
Other Java pages:
http://www.geocities.com/marcoschmidt.geo/java.html