使用Java填充属性文件的下拉列表 [英] Populate drop down from a properties file using Java

查看:97
本文介绍了使用Java填充属性文件的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 test.properties 文件,如下所示:

I have a test.properties file which looks like this:

1=abc
91=hjk
567=hello

我保存这在 IDEA 根目录中。我现在想学习如何以下拉选项的形式填充相同的属性文件,这应该像我的html格式一样:

I saved this in aIDEA root directory. I now wanted to learn how can I populate the same property file in a form of drop down options, which should go like this in my html form:

<select>
<option value=1>abc</option>
<option value=91>hjk</option>
.
.
.
</select>

所以应该是整数值这应该在中进行,并且键值文本将进入选项文本

so the key would be the integer value that should go in value and the key-value text would go in option Text.

我想使用 IDEA IDE在JAVA中执行此操作。任何帮助都会非常棒。

I wanted to do this in JAVA using IDEA IDE. Any help would be really great.

谢谢

Thanks

推荐答案

值得一提的是,无论您使用什么IDE,Java都是java。不太清楚你的根路径是什么意思。如果您的意思是项目根目录,则下面的代码将按原样运行。如果不是,则必须将test.properties更新为相对或绝对路径。

For what it's worth, java is java, regardless of what IDE you use. Not quite sure what you meant by "root path". If you mean the project root, than the code below will work as is. If not, you'll have to update "test.properties" to be a relative or absolute path.

        final StringBuilder buf = new StringBuilder("<select>");
        final Properties props = new Properties();
        InputStream is = null;
        try {
            is = new FileInputStream("test.properties");
            props.load(is);
            final Enumeration keyIter = props.propertyNames();
            while (keyIter.hasMoreElements()) {
                final String key = (String) keyIter.nextElement();
                // <option value=1>abc</option>
                buf.append("<option value=").append(key).append(">").append(props.getProperty(key))
                        .append("</option> ");
            }
        } finally {
            if (is != null) {
                is.close();
            }
        }
        buf.append("</select>");
        System.out.println(buf.toString());

结果

<select>
<option value=1>abc</option> 
<option value=567>hello</option> 
<option value=91>hjk</option> 
</select>

这篇关于使用Java填充属性文件的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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