合并Java中的两个Config.yml文件? [英] Merging two Config.yml Files in Java?

查看:312
本文介绍了合并Java中的两个Config.yml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对bukkit插件进行编程时,我意识到我需要拥有自己的配置文件,以便可以在文件中添加注释.我还需要能够更新配置(如果它已经创建并且很旧).

While programming my bukkit plugin, i realized that i needed to have my own config file so i can add comments into the file. I also needed to be able to update the config (if it has been created and is old).

我最近还完成了一个简单的jQuery插件,在其中使用了jQuery.extend并合并了两个设置数组.我想知道在带有配置文件的Java中是否可行.

I had also recently finished a simple jQuery plugin, where I used jQuery.extend and i merged two settings arrays. I wanted to know if this was possible in java with config files.

我的问题:

Is there a way i can merge the new default config with the one the user already has? (Removing non-needed values or changing the names of the same strings)

问题解释:

这是我可能有的config.yml示例:

Here is an example config.yml i might have:

# Comment here....

myString: defaultString 

myBool: false

myList: 
  - Value
  - Value 2

非常简单.可以说这是我的默认配置.插件已将此配置(如果尚未存在)复制到插件文件夹中.但是,这带来了一个问题:

Pretty simple. Lets say this is my default config. The plugin has copied this config (if it is not already there) inside the plugin folder. But, this brings up one issue:

What if i need to update my config? (Add/Remove a bool, string, etc.)

有一天,我说:我不再需要布尔myBool".我将其从默认的config.yml中删除,该配置看起来像这样:

One day, i say "I no longer need that boolean myBool". I remove it from the default config.yml and the config looks something like this:

# Comment here....

myString: defaultString 

myList: 
  - Value
  - Value 2

或者,我可能需要添加一个额外的字符串myNewString:

Or, i might need to add an extra string myNewString:

# Comment here....

myString: defaultString
myNewString: string 

myList: 
  - Value
  - Value 2

如果我将配置yml重写为新的默认"配置文件,我将丢失所有用户的配置设置.

If i rewrite the config yml to my new "Default" config file, i will lose all the user's configuration settings.

Is there a way i can merge the new default config with the one the user already has and just add the new string with the default values?

推荐答案

Bukkit具有内置的

Bukkit has a built in YamlConfiguration class with methods which allow you to get a value or specify a default value to retrieve if none exist, such as getString(String path, String default).

按路径获取请求的字符串,如果找不到,则返回默认值.

Gets the requested String by path, returning a default value if not found.

如果字符串不存在,则无论是否在根配置中标识了默认值,都将返回指定的默认值.

If the String does not exist then the specified default value will returned regardless of if a default has been identified in the root Configuration.

但是,它没有从配置中删除值的方法.如果新配置与前一个配置有很大不同,则可以考虑创建新配置,删除旧配置并重命名新配置来代替它.尽管除非有明显不同,否则我不会太在意它.

However it does not have a way to remove values from the configuration. If the new configuration is so different from the previous one, you might consider creating the new configuration, deleting the old and renaming the new to take it's place. Though I wouldn't be too concerned about it unless it's significantly different.

这篇关于合并Java中的两个Config.yml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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