java - NIO 持久化数组 : Java

标签 java arrays persistence nio

在我的“高级”Java 类(class)中,我被分配了创建 NIO 持久性数组的任务。这个问题与其说是“如何”做到这一点,不如说是“NIO Persistent”隐含的“什么”。这对我来说是一个新概念;到目前为止,我明白这基本上只是意味着不是在代码中的典型数组中动态存储信息,而是将数组的信息以预先确定大小的块中的字节形式存储在磁盘上。我感到困惑的是如何从试图存储的信息中获取字节,然后如何通过 Java 的 NIO 将这些字节写入文件?

TL;DR - 有人可以简单地解释一下 Java NIO 持久性数组吗?我应该如何从头开始构建一个?

感谢您提前提出的任何意见!

编辑:这是作业描述,逐字逐句:

Objective

Become familiar with Java's NIO API.

Requirements

Use Java's NIO to create a PersistentArray class. The class should support the following methods:

static void create(String fileName, int bufferSize) - creates an empty persistent array

static void delete(String fileName) - deletes the persistent array

PersistentArray open(String fileName) - opens the file associated with the persistent array and prepares the persistent array for gets and puts.

void put(int bufferID, Buffer buffer) - Stores the buffer at the bufferID's location (note that the buffer size must be the same as the size used when creating the array).

Buffer get(int bufferID) - given the buffer ID, retrieve the buffer previously stored at the location associated with bufferID.

int getNextID() - return one beyond the maximum ID ever used for storing a buffer .

void close() - close the file associated with the persistent array

Also, create a JUnit test showing that all methods behave as expected.

Review your work with the instructor.

最佳答案

什么:
没有像“NIO 持久数组”这样的东西。讲师希望您熟悉 NIO API(因为它比旧的 IO API 更好更快)。

现在在作业的上下文中,您需要实现这个符合教师指定规范的类。没有花哨的东西,只需使用 nio 实现一个将数据(在您的情况下为数组)“持久化”到文件的类。

这篇文章可能对你有帮助。 File I/O: old I/O or NIO. Which is better?

TL;DR: There is no special meaning to NIO persistence. Just create an array which is not in memory, but also persists its content using NIO.



如何:
现在,它可以通过多种方式实现,Peter 的回答非常好,建议使用 RandomAccessFile。

关于java - NIO 持久化数组 : Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7863963/

相关文章:

java - Hibernate 复制问题—— session 真的需要复制吗?

java - Lucene 4.0 IndexWriter updateDocument for Numeric Term

javascript - 使用 every 时如何跳过一个元素

java - 更新依赖于其他实体状态的@Entity 字段的正确方法

c# - 将文本文件中的每一行放入数组 C#

c - 将整个数组传递给 C 中的函数

java - Spring Data JPA连接2个表

java - 使用哪个 Java XML RPC 库

java - Spring Boot - Stomp : '.' vs '/' in Message Destination

java - Java 中的别名分析