简体   繁体   English

Java读取和写入文件的对象

[英]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. 我的任务是创建一个类(RandomAccessObjectFile),它提供对象的读取和写入以及在文件中搜索。 This is my first time using Java File NIO and want to make sure I am grasping it correctly. 这是我第一次使用Java File NIO,并希望确保我正确掌握它。 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. 有关java.nio包的背景知识,您可以从java教程开始 To serialize an object to a File, I've used ObjectOutputStream with good success. 为了将对象序列化为File,我使用了ObjectOutputStream并取得了很好的成功。 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. seek方法的大多数输入值都是无效的,因为文件中的特定位置可能不代表对象序列化的开始。 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. 但是如果这是解释输入参数的方式,那么我认为如果输入值不代表Object的开头,该方法应抛出IllegalArgumentException。

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. 也许写入文件的每个对象都需要有一些唯一的ID字段。 Then create a map that maps these IDs to positions in the file. 然后创建一个映射,将这些ID映射到文件中的位置。 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. 除了PathFiles的琐碎和冗余使用之外,这里根本没有NIO,所以很难看出你的问题实际上是什么。 However I would comment as follows: 不过我会评论如下:

  1. The create() method is redundant. create()方法是多余的。 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. 鉴于File.delete()Files.delete()已经存在, delete()方法是多余的。

  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. 除非您知道在哪里寻求,否则您无法寻找它们,也没有提供API来获取该信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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