发布于2022-06-24 17:02 阅读(692) 评论(0) 点赞(4) 收藏(4)
我对 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 到对象序列化器/反序列化器。
例如(如网页中所述):
首先,您必须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 非常适合对于表格内容。
然而,一个缺点是数据结构是二进制编码的:它们不容易读取。尽管在这种情况下这可能是一个优势,因为一些用户倾向于更改文件以提高他们的高分;)。
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/463667/74e7a17efe6e6d7cd2fa/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!