简体   繁体   中英

Java reading and writing objects to a file

I have been tasked with creating a class (RandomAccessObjectFile) that provides reading and writing of objects as well as seeking within a file. This is my first time using Java File NIO and want to make sure I am grasping it correctly. Below are the methods I am required to have in the class and am looking for some help and more understanding on how to approach the rest of this class.

public class RandomAccessObjectFile {

RandomAccessFile raFile;
private RandomAccessObjectFile(String fileName) throws FileNotFoundException {
    raFile = new RandomAccessFile(new File(fileName), "rw");
}

static void create(String fileName) throws IOException {
    Path createTarget = Paths.get(fileName);
    Files.createFile(createTarget);
}

static void delete(String fileName) throws IOException {
    Path deleteTarget = Paths.get(fileName);
    Files.delete(deleteTarget);
}

static RandomAccessObjectFile open(String fileName) {

}

<T> void write(T obj) {
}

<T> T read() {

}

void seek(long location) {
}

long length() {

}

}

Thanks ahead of time for all of your help!

For background on the java.nio package you could start with the java tutorial. To serialize an object to a File, I've used ObjectOutputStream with good success. I think you may need some further clarification on what the seek method should do. Is the long input a File location? That seems like an odd thing to do since Objects written to the file will occupy various amounts of room in the file. Most inputs values to the seek method would be invalid since a particular location in the file may not represent the beginning of an object serialization. But if this is how the input parameter should be interpreted, then I think the method should throw an IllegalArgumentException if the input value does not represent the start of an Object.

How will a user of this class know how to seek a particular Object?

Maybe you should also store some index data. Maybe each object written to the file needs to have some unique ID field. Then create a map that maps these IDs to positions in the file. This map can then be used to quickly jump to the correct file location to read data. This approach should work well as long as the data in the file does not need to change. Adding new Objects to the file this way should work fine, but modifying an object or deleting one from the middle of the file would require more work.

There is no NIO here at all apart from the trivial and redundant use of Path and Files , so it's hard to see what your question is actually about. However I would comment as follows:

  1. The create() method is redundant. The target file will be created when you construct an instance of your class, or else an exception will be thrown.

  2. The delete() method is redundant given that File.delete() and Files.delete() already exist.

  3. The entire assignment is futile. Object streams are not random access. They are strictly streams. You cannot seek them unless you know where to seek to, and no API is provided to obtain that information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM