Hi, I am trying to write a program having file handling. In it, I write an object in a .dat file. Is there any mechanism to read a specific object from a file? Is there any method like 'fseek()' as provided in C?

i don't know how to read .dat file but java.util.Scanner is used to read .txt files. Just for an idea

You can read/write a RandomAccessFile using your own code to convert your objects to/from bytes of data.
You can use Java ObjectOutputStream / ObjectInputStream to write/read simple or complex objects (including arrays & collections) with a single statement.
You can use XMLEncoder /XMLDecoder to encode/decode simple or complex objects (including arrays & collections) as XML text that you can write/read to file.

ps Classes (names starting with a capital letter) above are fully documented in the JAVA API doc.

Edited 5 Years Ago by JamesCherrill: ps

Is there any mechanism to read a specific object from a file? I

I think you have to read the file sequentially from the beginning. If the file has more than one object then you will need to read the ones before the desired object to get to the desired object.

The reference to fseek leads me to suspect a scenario where objects are written as fixed-length records, so it would be possible to use the java.io.RandomAccessFile seek(pos) method to seek directly to (record number)*(record length). Although I wouldn't recommend that kind of low-level approach unless the file size is humongous.

Edited 5 Years Ago by JamesCherrill: n/a

This question has already been answered. Start a new discussion instead.