使用超类和扩展类方法写入随机访问文件

发布于 2024-12-17 21:01:02 字数 1866 浏览 1 评论 0 原文

我有一个 Book 类和一个扩展 Book 的图书馆图书类。我将信息存储在随机访问文件中。我有一个 writeToFile 方法,它将 Book 对象写入随机访问文件。我的 LibraryBook 类的方法 writeToFile 调用 super.writeToFile,然后我希望它将特定于 LibraryBook 的字段写入文件。执行此操作的正确方法是什么?请参阅代码:

书籍中的方法 类:

public void writeToFile(String fileName, long location) throws Exception {
    try {
        RandomAccessFile invFile = new RandomAccessFile(fileName, "rw");
        // seek to correct record in file
        invFile.seek(location);
        // now write out the record
        // write out the data in fixed length fields
        // String fields must be truncated if too large
        // or padded with blanks if too small
                       //write out all Book variable to file
        invFile.writeLong(ISBN);
        //etc.....          

    } catch (FileNotFoundException notFound) {
        throw new FileNotFoundException();
    } catch (IOException io) {
        throw io;
    }
}

扩展 Book 的 LibraryBook 类中的方法:

    public void writeToFile(String fileName, long location) throws Exception {
    try {
        super.writeToFile(fileName, location);
        RandomAccessFile invFile = new RandomAccessFile(fileName, "rw");

        // seek to correct record in file
        invFile.seek(location);
        // now write out the record
        // write out the data in fixed length fields
        // String fields must be truncated if too large
        // or padded with blanks if too small
                       //write library book variables to file
        invFile.writeLong(branchID);
                        //etc....

                    invFile.close();
    } catch (FileNotFoundException notFound) {
        throw new FileNotFoundException();
    } catch (IOException io) {
        throw io;
    }
}

如何对其进行编码,以便 LibraryBook writeToFile 方法可以调用超类方法并将 LibraryBook 保存到文件中?

I have a Book class and a Library Book class which extends Book. I am storing the information in a random access file. I have a writeToFile method which writes Book objects to a random access file. My LibraryBook class's method writeToFile calls super.writeToFile and then I want it to write the fields specific to the LibraryBook to the file. What is the proper way to do this? see code:

method from the book class:

public void writeToFile(String fileName, long location) throws Exception {
    try {
        RandomAccessFile invFile = new RandomAccessFile(fileName, "rw");
        // seek to correct record in file
        invFile.seek(location);
        // now write out the record
        // write out the data in fixed length fields
        // String fields must be truncated if too large
        // or padded with blanks if too small
                       //write out all Book variable to file
        invFile.writeLong(ISBN);
        //etc.....          

    } catch (FileNotFoundException notFound) {
        throw new FileNotFoundException();
    } catch (IOException io) {
        throw io;
    }
}

method from the LibraryBook class which extends Book:

    public void writeToFile(String fileName, long location) throws Exception {
    try {
        super.writeToFile(fileName, location);
        RandomAccessFile invFile = new RandomAccessFile(fileName, "rw");

        // seek to correct record in file
        invFile.seek(location);
        // now write out the record
        // write out the data in fixed length fields
        // String fields must be truncated if too large
        // or padded with blanks if too small
                       //write library book variables to file
        invFile.writeLong(branchID);
                        //etc....

                    invFile.close();
    } catch (FileNotFoundException notFound) {
        throw new FileNotFoundException();
    } catch (IOException io) {
        throw io;
    }
}

how can I code it so that the LibraryBook writeToFile method can call the superclass method and save a LibraryBook to the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最偏执的依靠 2024-12-24 21:01:02

您所做的本质上是将记录存储到文件中;已经有许多机制可用于将结构化数据存储到文件中。如果您有兴趣这样做是为了学习,那么无论如何都要继续——但如果您只是想解决这个问题,请考虑使用类似 SQLite3 为您提供存储。

如果您想继续使用此方法,则需要确定每个方法将使用多少大小,将文件的一部分分配给每个要更新的方法,并确保每个方法确切地知道文件中的位置它的位置。

如果这只是一个 C 程序,我建议计算每个方法负责的字段、#define 这些大小以及 #define 要添加到 的偏移量每个位置

但这感觉不太“Java”——您应该能够修改父类、子类,或者完全添加新类,而无需了解其他类的详细信息。

因此,您可能希望让一个类负责将结构化数据写入文件并查询涉及的每个类的数据。让每个类返回一个它们想要写入的字节数组,并让一个主类执行所有写入操作。

通过将所有文件 IO 整合到一个类中,您以后可以更轻松地更改为另一种存储格式,或者保持程序的两个或三个先前版本的兼容性,或者提供多个数据库后端以满足部署时的不同需求。

What you're doing is essentially storing records into the file; there are many mechanisms already available for storing structured data into files. If you're interested in doing this for learning, then by all means keep going -- but if you just want this problem solved, look into using something like SQLite3 to provide your storage for you.

If you want to continue with this approach, you need to determine how much size each of your methods will use, allocate a portion of the file to each of the methods to update, and make sure that each one knows exactly where in the file is its location.

If this were just a C program, I'd suggest counting the fields each method is responsible for, #define those sizes and #define the offsets to add to location for each one.

But that doesn't feel very "Java" -- you're supposed to be able to modify parents, children, or add new classes entirely, without knowing details about the other classes.

So you might want to put one class in charge of writing structured data to files and query each of the classes involved for their data. Have each class return an array of bytes that they want written -- and have the one master class perform all the writing.

By consolidating all your file IO in one class, you can more easily change to another storage format later on, or maintain compatibility for two or three previous versions of your program, or provide multiple database backends to suit different needs at deployment.

阿楠 2024-12-24 21:01:02

将您的 writeToFile() 方法设为最终方法。

然后添加一个受保护的 writeExtraData() 方法,该方法对于书籍不执行任何操作,但会被重写以在 LibraryBook 类中写入额外的字段。从 Book 中的 writeToFile() 方法中间调用此函数。显然,您需要实现一组对称的读取方法。

更好的是,停止重新发明轮子和编写令人讨厌的样板代码,只需使用内置的 ObjectOutputStreamObjectInputStream 语言提供的用于执行此类操作的类。然后你就可以使用readObject()

Make your writeToFile() method final.

Then add a protected writeExtraData() method which does nothing in the case of a book, but is overridden to write the extra fields in the LibraryBook class. Call this from the middle of the writeToFile() method in Book. You will obviously need to implement a symmetric set of read methods.

Better yet, stop reinventing the wheel and writing nasty boilerplate code and just use the built-in ObjectOutputStream and ObjectInputStream classes that the language provides to do this sort of thing. Then you can just go readObject().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文