读取具有特定字符串的 java 中的属性文件 [英] Read properties file in java having particular string

查看:46
本文介绍了读取具有特定字符串的 java 中的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个 .properties 文件.因为我有以下配置参数:

I am using one .properties file. In that I have following config parameters :

Appnameweb = app1
Appnamemobile = app2
Appnameweb1 = app3

可以有很多以用户提供的 Appname 开头的配置参数.如何读取其中键将包含特定字符串的所有属性文件参数,如本例中的 Appname?

There can be many config param starting with Appname provided by user. How to read all properties file parameters in which key will contain particular String like in this case Appname?

推荐答案

一般看一下java.util.Properties的javadoc.为了让您的生活更轻松,我会给您这个代码片段,可以帮助您开始:

Generally take a look on javadoc of java.util.Properties. To make you life easier I will give you this code snippet that can help you to start:

Properties props = new Properties();
props.load(new FileInputStream("myfile.properties"));
for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); ) {
    String name = (String)e.nextElement();
    String value = props.getProperty(name);
    // now you have name and value
    if (name.startsWith("Appname")) {
        // this is the app name. Write yor code here
    }
}

这篇关于读取具有特定字符串的 java 中的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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