开发者

seeking java access file position, how to learn size of record

I want to move file descriptor to beginning of the record. I have a class named Record. For example, according to parameter, lets say 4, i want to access beginning of the 4. record. In this way, I think I will use seek method. But i need size of Record. in c language, I can say that go to

f seek开发者_如何学编程(3*(size of(Record)))

but in Java, how could not find how to learn my Record size. So how can i do this?


You can serialize your object to a ByteArrayOutputStream then call the toByteArray() method of the stream to get the byte array itself, then you can easily get the size in bytes by calling length on the array.
The byte array is what you want to write to the file.
If you look at http://www.coyotegulch.com/products/itzam/java/index.html you can find a database whose basic implementation is a linked list of arbitrary objects in a file. The author then proceeds to use an indexing scheme on top of that for keyed access.
I have used this before and it works - It might beat writing your own.


What have you used to write the data? There isn't any way to know how large a record is if you are using ObjectOutputStream, and you can't seek, and load objects randomly in a file written by ObjectOutputStream.


How about first putting the records in a skip list datastructure and writing out the skiplist as file offsets with the records to disk. You can take one pass over the data to find their serialized lengths beforehand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜