将Java数组存储到文件中以进行读写 [英] Store java arrays to file for read and write

查看:91
本文介绍了将Java数组存储到文件中以进行读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对StackOverflow还是很陌生,所以很抱歉如果这个问题写得不正确。

I am fairly new to StackOverflow so I'm sorry if this question is not written correctly.

我正在学习Java,正在寻找一个一种将一组数组存储为文件的方法,以便对数组的更改将在程序的不同实例之间持久存在。我需要阅读一份对最佳时间列表所做的任何更改,然后再写入文件。

I'm currently learning java and I'm looking for a way to store a set of arrays as a file so that changes to the array will persist between different instances of the program. I would need to read an write any changes to the best times list to a file.

我已经对存储数组进行了一些研究,但大部分内容似乎仅存储字符串,整数等的简单数组。这些阵列稍微复杂一点,所以我不确定该怎么做。
我不希望有人为我编写所有代码,但是我可以在了解从何处开始时使用一些帮助。

I have done some research into storing arrays but most of what I've read seems to only store a simple array of strings, integers, ect. These arrays are a little bit more complex so I'm not sure how to do it. I don't expect anyone to write all of my code for me but I could use some help on knowing where to start.

这是示例数据那将需要存储。这是Java游戏的最佳时机。

This is the sample data that would need to be stored. These are best times for a java game.

是否存在将此类数据存储在某种文件中的方法?

Are there methods to store this kind of data in a file of some sort?

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;
    }
}


推荐答案

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

In order to store an object (datastructure), you need to serialize it: convert it to an stream of bytes, characters, whatever.

有存在多种方法来序列化对象,从XML上的JSON到 对象序列化器/反序列化器

There exist multiple ways to serialize an object ranging from JSON over XML to the object serializer/deserializer.

例如(在网页中指定):

For instance (as specified in the webpage):

首先,您必须在类定义的末尾添加可序列化的实现,因此:

First you must add implements Serializable at the end of the class definition, so:

public class BestTimes implements Serializable {

您可以使用以下命令进行序列化:

Then you can serialize using:

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不能像处理循环一样

The easy thing about the object serializer is that you don't have to define a format on your own. That's the task of the Java virtual machine, and furthermore it can encode all kinds of objects whereas JSON for instance can't handle datastructures that contain loops (example: graphs), XML can't handle loops as well and CSV is only well suited for table content.

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

A disadvantage however is that the datastructures are encoded binary: they cannot be read easily. Although in this case that might be an advantage since some users tend to alter the file to increase their highscores ;).

这篇关于将Java数组存储到文件中以进行读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆