java - 将 java 数组存储到文件中以供读写

标签 java arrays file

我是 StackOverflow 的新手,所以如果这个问题写得不正确,我很抱歉。

我目前正在学习 Java,并且正在寻找一种将一组数组存储为文件的方法,以便对数组的更改将在程序的不同实例之间持续存在。我需要将最佳时间列表的任何更改读取并写入文件。

我对存储数组做了一些研究,但我读过的大部分内容似乎只存储一个简单的字符串数组、整数等。这些数组有点复杂,所以我不确定该怎么做。 我不希望任何人为我编写所有代码,但我可以使用一些帮助来了解从哪里开始。

这是需要存储的样本数据。现在是玩 Java 游戏的最佳时机。

有没有办法将这种数据存储在某种文件中?

public class BestTimes 
    {
        BestTimes[] beginner = new BestTimes[10];
        BestTimes[] intermediate = new BestTimes[10];
        BestTimes[] expert = new BestTimes[10];

        public BestTimes() {
        beginner[0] = new BestTimes(1, "John", 10.5);
        beginner[1] = new BestTimes(2, "James", 20.3);
        beginner[2] = new BestTimes(3, "Jill", 30);
        beginner[3] = new BestTimes(4, "Bill", 35);
        beginner[4] = new BestTimes(5, "Kevin", 40);
        beginner[5] = new BestTimes(6, "Nate", 55);
        beginner[6] = new BestTimes(7, "Bob", 75);
        beginner[7] = new BestTimes(8, "Dan", 85);
        beginner[8] = new BestTimes(9, "Amy", 93);
        beginner[9] = new BestTimes(10, "Jane", 100);

        intermediate[0] = new BestTimes(1, "John", 110.5);
        intermediate[1] = new BestTimes(2, "James", 120.3);
        intermediate[2] = new BestTimes(3, "Jill", 130);
        intermediate[3] = new BestTimes(4, "Bill", 135);
        intermediate[4] = new BestTimes(5, "Kevin", 140);
        intermediate[5] = new BestTimes(6, "Nate", 155);
        intermediate[6] = new BestTimes(7, "Bob", 175);
        intermediate[7] = new BestTimes(8, "Dan", 185);
        intermediate[8] = new BestTimes(9, "Amy", 193);
        intermediate[9] = new BestTimes(10, "Jane", 200);

        expert[0] = new BestTimes(1, "John", 210.5);
        expert[1] = new BestTimes(2, "James", 220.3);
        expert[2] = new BestTimes(3, "Jill", 230);
        expert[3] = new BestTimes(4, "Bill", 235);
        expert[4] = new BestTimes(5, "Kevin", 240);
        expert[5] = new BestTimes(6, "Nate", 255);
        expert[6] = new BestTimes(7, "Bob", 275);
        expert[7] = new BestTimes(8, "Dan", 285);
        expert[8] = new BestTimes(9, "Amy", 293);
        expert[9] = new BestTimes(10, "Jane", 300);
        }

    public int ranking;
    public String playerName;
    public double time;

    public BestTimes(int r, String p, double t) 
    {
        ranking = r;
        playerName = p;
        time = t;
    }
}

最佳答案

为了存储对象(数据结构),您需要对其进行序列化:将其转换为字节流、字符流等。

有多种方法可以序列化对象,从 JSON over XML 到 object serializer/deserializer .

例如(在网页中指定):

首先你必须在类定义的末尾添加implements Serializable,所以:

public class BestTimes implements Serializable {

然后你可以序列化使用:

try(FileOutputStream f = new FileOutputStream("file.txt");
    ObjectOutput s = new ObjectOutputStream(f)) {
    s.writeObject(beginner);
    s.writeObject(intermediate);
    s.writeObject(expert);
}

然后将其解读为:

BestTimes[] beginner, intermediate, expert;
try(FileInputStream in = new FileInputStream("file.txt");
    ObjectInputStream s = new ObjectInputStream(in)) {
    beginner = (BestTimes[]) s.readObject();
    intermediate = (BestTimes[]) s.readObject();
    expert = (BestTimes[]) s.readObject();
}

对象序列化器的简单之处在于您不必自己定义格式。这是 Java 虚拟机的任务,而且它可以编码所有 类型的对象,而 JSON 例如不能处理包含循环的数据结构(例如:图形),XML 不能处理循环作为嗯,CSV 仅适用于表格内容。

然而,缺点是数据结构是二进制编码的:它们不容易读取。虽然在这种情况下这可能是一个优势,因为一些用户倾向于更改文件以增加他们的高分;)。

关于java - 将 java 数组存储到文件中以供读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28570967/

相关文章:

java - 导出 RCP 应用程序时出现问题

Ruby MRI 1.8.7 - 文件写入线程安全

java - 如何在注释中使用数组常量

java - 使用 getter setter 初始化 List

python - 如何从索引矩阵中提取列?

python - 类型错误 : Invalid dimensions for image data when plotting array with imshow()

java - 未使用的基元数组 : what do javac and the JIT compiler do with it?

javascript - 读/写 Windows 图像标签(关键字)

C Readdir 不读取所有文件

java - 如何将 JLabels 定位到 Java GUI 上的绝对位置