[3294] in java-interest
Re: a question about structures
daemon@ATHENA.MIT.EDU (Ken Arnold - Sun Labs)
Mon Nov 6 17:46:21 1995
Date: Mon, 6 Nov 1995 14:53:57 -0500
From: arnold@cocolat.East.Sun.COM (Ken Arnold - Sun Labs)
To: java-interest@webrunner.neato.org
michaels@pallas.statenet.com asks:
>I realize that java does not handle structures, so what is the best way
>to handle data when you need something like a C like structure to
>handle the organization of small files that hold specific data. I am
>writing an applet that will need to utilize small file I/O, while being
>able to get to certian parts of the data quickly to perform read and
>writes.
You create a class that has only public data members. That is the
struct equivalent. You create a RandomAccessFile so you can seek
around in the file you want to write them in. You provide readFrom()
and writeTo() methods on your "struct" that take a RandomAccessFile as
a parameter and use the DataInput and DataOutput methods to read/write
the data in your "struct".
For example, something like the following would work:
import java.io.*;
public class Point {
public int x, y;
public void readFrom(RandomAccessFile file) throws IOException {
x = file.readInt();
y = file.readInt();
}
public void writeTo(RandomAccessFile file) throws IOException {
file.writeInt(x);
file.writeInt(y);
}
}
Then you could seek around reading and writing the things into a file.
Alternatively you could rely completely on native methods that used
stdio or posix calls, but, well, to be blunt, *bletch* the above really
won't meet your performance requirements.
Enjoy,
Ken Arnold
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com