gpt4 book ai didi

Java YAML 读取动态变化文件的最佳方式

转载 作者:行者123 更新时间:2023-11-30 07:40:26 24 4
gpt4 key购买 nike

我正在尝试找到用 Java 读取 YAML 文件的最佳方法。我不确定哪个解析器是最好使用的,但目前我的读取动态的解决方案(如果用户想向文件添加更多内容,下次程序加载时)是一系列 for 循环,转换为 和从 ArrayList 和 hashmap 并解析原始字符串。

例如:

HashMap<Object, Object> perWaveHashMap = (HashMap)waveConfiguration.getList("waves").get(wave); //wave-1
for(Object mobObject : perWaveHashMap.values()){
ArrayList<Object> mobObjectArrayList = (ArrayList) mobObject;
for(Object mob : mobObjectArrayList){
HashMap<Object, Object> mobHashMap = (HashMap)mob;
String mobNameString = null;
for(Object mobName : mobHashMap.keySet()){
mobNameString = mobName.toString();
}
for(Object mobAttribute : mobHashMap.values()){
HashMap<Object, Object> attributesToGive = (HashMap)mobAttribute;
final ArrayList<Object> attributeList = new ArrayList<>();
for(Object attribute : attributesToGive.values()){
attributeList.add(attribute);
}
assert mobNameString != null;
final String finalMobNameString = mobNameString;

System.out.println("Final Mob Name String: " + finalMobNameString);
if(finalMobNameString.equalsIgnoreCase("vintr")){
Mobs.isSpecialBossWave =true;
new Vintr();
} else {
if (attributeList.size() == 1) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()));
} else if (attributeList.size() == 2) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1));
} else if (attributeList.size() == 3) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2));
} else if (attributeList.size() == 4) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2), (boolean) attributeList.get(3));
} else if (attributeList.size() == 5) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2), (boolean) attributeList.get(3), (boolean) attributeList.get(4));
} else if (attributeList.size() == 6) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2), (boolean) attributeList.get(3), (boolean) attributeList.get(4), (boolean) attributeList.get(5));
}
}
}
}
}

基本上,YML 文件如下所示:

waves:
- 1:
- zombie:
amount: 1
modifier: 2
boss: false
beeline: false
- 1:
- spider:
amount: 12
modifier: 2
boss: false
beeline: false
hunter: true
-2 :
- mobname:
-options

我的代码现在的方式允许我添加任意数量的“波”,以及我想要在每个波中添加的小怪。我发现我编写代码的方式非常丑陋且难以使用,并且正在寻找更好的方法来解决这个问题。

欢迎任何帮助!

谢谢

最佳答案

您可以使用snakeyaml将yaml文件读入POJO。

File configFile = new File(configFilename);
FileInputStream fis = new FileInputStream(configFile);
Yaml yaml = new Yaml(new Constructor(Config.class));
Config config = yaml.loadAs(fis, Config.class);

您的 Config 类可以是这样的:

public class Config {

List<Wave> waves;

public List<Wave> getWaves() {
return waves;
}
}

// similarly define other classes for the Wave etc...

阅读文档:https://bitbucket.org/asomov/snakeyaml/wiki/Home

关于Java YAML 读取动态变化文件的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34774282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com