从prop文件中提取信息

时间:2014-10-28 17:14:34

标签: java android properties extract

我有一个prop文件,我想从中提取版本发布号(作为String)。 该文件如下所示:

ro.build.version.sdk=19

ro.build.version.codename=REL

ro.build.version.release=4.4.2

ro.build.date=Mon Oct 27 00:53:09 IST 2014

ro.build.date.utc=1414363989

我不知道版本发布号,所有我都知道我的文件上有这个道具。 如何使用Java获得此prop的值,并将其保存在String变量中? (这里将是4.4.2)

1 个答案:

答案 0 :(得分:0)

像往常一样读入文件并将每一行存储在一个arraylist中。

ArrayList<String> lines; //store the lines here
String releaseNum = "";

然后循环:

    for(String s : lines){
        keyVal = s.split("="); // keyVal is now an array of len 2, keyVal[0] being the key, keyVal[1] being the value.
        if(keyVal[0].equals("ro.build.version.release")){
            releaseNum = keyVal[1]; //got the value
        }
    }

现在releaseNum是您要检索的值。

如果这是android,请使用Properties类。 例如:

        Properties prop = new Properties();
        String propFileName = "fileName.properties";

        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
        prop.load(inputStream);
        if (inputStream == null) {
            throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
        }
        String releaseNum = prop.getProperty("ro.build.version.release");