将Java数组存储到文件中以进行读写

kb4*_*000 1 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*sem 5

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

有多种方法可以序列化对象,从XML之上的JSON到对象序列化器/反序列化器

例如(在网页中指定):

首先,您必须implements Serializable在类定义的末尾添加,因此:

public class BestTimes implements Serializable {
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下方法进行序列化:

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

后来读为:

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();
}
Run Code Online (Sandbox Code Playgroud)

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

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