简体   繁体   English

JAVA:将大型配置文件加载到代码中-如何存储它?

[英]JAVA: Loading a big configuration file to code - How to store it?

I have big configuration file to my bukkit plugin but I don't know how to create a config class for it. 我的bukkit插件有很大的配置文件,但是我不知道如何为它创建一个配置类。 If I create one class with that amount of variables then is hard to use and read/edit. 如果我用这么多的变量创建一个类,那么将很难使用和读取/编辑。

So I have new idea using inner-clesses but how about performance etc? 所以我对使用内部消息有了新的想法,但是性能如何呢?

That example configuration file: (in YAML, and the real configuration file is much bigger) http://pastebin.com/09WMq5qG 该示例配置文件:(在YAML中,实际的配置文件要大得多) http://pastebin.com/09WMq5qG

And my code to that example: 我的示例代码:

public class Cfg {
    protected General general;
    protected General2 general2;

    public Cfg(ConfigurationSection cfg) {
        general = new General(cfg.getConfigurationSection("General"));
        general2 = new General2(cfg.getConfigurationSection("General2"));
    }

    public class General {
        protected Gen2 gen2;
        protected Gen5 gen5;

        protected int op1;
        protected String op2;

        public General(ConfigurationSection general) {
            this.op1 = general.getInt("op1");
            this.op2 = general.getString("op2");
            gen2 = new Gen2(general.getConfigurationSection("Gen2"));
            gen5 = new Gen5(general.getConfigurationSection("Gen5"));
        }

        public class Gen2 {
            protected Gen3 gen3;
            protected Gen4 gen4;

            protected int lol;
            protected String edrd;

            public Gen2(ConfigurationSection gen2) {
                this.lol = gen2.getInt("lol");
                this.edrd = gen2.getString("edrd");
                gen3 = new Gen3(gen2.getConfigurationSection("Gen3"));
                gen4 = new Gen4(gen2.getConfigurationSection("Gen4"));
            }

            public class Gen3 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen3(ConfigurationSection gen3) {
                    this.dsdgdf = gen3.getInt("dsdgdf");
                    this.djkw4g = gen3.getString("djkw4g");
                }
            }

            public class Gen4 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen4(ConfigurationSection gen4) {
                    this.dsdgdf = gen4.getInt("dsdgdf");
                    this.djkw4g = gen4.getString("djkw4g");
                }
            }
        }

        public class Gen5 {
            protected Gen6 gen6;
            protected Gen7 gen7;

            protected int lol;
            protected String edrd;

            public Gen5(ConfigurationSection gen5) {
                this.lol = gen5.getInt("lol");
                this.edrd = gen5.getString("edrd");
                gen6 = new Gen6(gen5.getConfigurationSection("Gen6"));
                gen7 = new Gen7(gen5.getConfigurationSection("Gen7"));
            }

            public class Gen6 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen6(ConfigurationSection gen6) {
                    this.dsdgdf = gen6.getInt("dsdgdf");
                    this.djkw4g = gen6.getString("djkw4g");
                }
            }

            public class Gen7 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen7(ConfigurationSection gen7) {
                    this.dsdgdf = gen7.getInt("dsdgdf");
                    this.djkw4g = gen7.getString("djkw4g");
                }
            }
        }
    }

    public class General2 {
        protected Gen2 gen2;
        protected Gen5 gen5;

        protected int op1;
        protected String op2;

        public General2(ConfigurationSection general2) {
            this.op1 = general2.getInt("op1");
            this.op2 = general2.getString("op2");
            gen2 = new Gen2(general2.getConfigurationSection("Gen2"));
            gen5 = new Gen5(general2.getConfigurationSection("Gen5"));
        }

        public class Gen2 {
            protected Gen3 gen3;
            protected Gen4 gen4;

            protected int lol;
            protected String edrd;

            public Gen2(ConfigurationSection gen2) {
                this.lol = gen2.getInt("lol");
                this.edrd = gen2.getString("edrd");
                gen3 = new Gen3(gen2.getConfigurationSection("Gen3"));
                gen4 = new Gen4(gen2.getConfigurationSection("Gen4"));
            }

            public class Gen3 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen3(ConfigurationSection gen3) {
                    this.dsdgdf = gen3.getInt("dsdgdf");
                    this.djkw4g = gen3.getString("djkw4g");
                }
            }

            public class Gen4 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen4(ConfigurationSection gen4) {
                    this.dsdgdf = gen4.getInt("dsdgdf");
                    this.djkw4g = gen4.getString("djkw4g");
                }
            }
        }

        public class Gen5 {
            protected Gen6 gen6;
            protected Gen7 gen7;

            protected int lol;
            protected String edrd;

            public Gen5(ConfigurationSection gen5) {
                this.lol = gen5.getInt("lol");
                this.edrd = gen5.getString("edrd");
                gen6 = new Gen6(gen5.getConfigurationSection("Gen6"));
                gen7 = new Gen7(gen5.getConfigurationSection("Gen7"));
            }

            public class Gen6 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen6(ConfigurationSection gen6) {
                    this.dsdgdf = gen6.getInt("dsdgdf");
                    this.djkw4g = gen6.getString("djkw4g");
                }
            }

            public class Gen7 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen7(ConfigurationSection gen7) {
                    this.dsdgdf = gen7.getInt("dsdgdf");
                    this.djkw4g = gen7.getString("djkw4g");
                }
            }
        }
    }
}

Using this is easier for me than storing all variables in one class but how about performance? 与将所有变量存储在一个类中相比,使用它对我来说更容易,但是性能如何? That will be good method or just slow down the code? 那将是个好方法还是只是减慢代码速度?

//Sorry for my English. //对不起我的英语不好。

What you could do: Load the configuration file and store the whole content in one variabel (array, ArrayList, List, Hashmap, whatever suits your particular needs). 您可以做什么:加载配置文件并将全部内容存储在一个variabel中(数组,ArrayList,List,Hashmap,任何适合您特定需求的东西)。 After this, all your classes are able to read it. 之后,您所有的班级都可以阅读它。 You can either enforce a clear order in your configuration file content, so that parts of your program know exactly, for example, in which line/array index range they have to search for the information they need. 您可以在配置文件内容中强制执行清晰的顺序,以便程序的某些部分准确地知道,例如,他们必须在哪个行/数组索引范围内搜索所需的信息。 You could also define a method which distributes all the configuration details to previously initialized parts of your program. 您还可以定义一个将所有配置详细信息分发到程序的先前初始化部分的方法。 It is all up to you. 所有决定权都在你手里。 Because you didn't pointed out preciyle what you intend to do I can't do in much more detail here either. 因为您没有指出麻烦您打算做什么,所以我在这里也无法做更多详细的说明。

However, I would suggest that you stay away from creating classes inside classes inside classes and so on. 但是,我建议您不要在类内部的类内部创建类。 Java gives you great object-oriented tools to help you organize your code in small readable portions. Java为您提供了出色的面向对象的工具,可帮助您将代码组织成小的可读部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM