2014-10-28 124 views
0

我收到了一個prop文件,我想從中提取版本號(作爲字符串)。 該文件是這樣的:從prop文件中提取信息

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獲得這個道具的價值,並將其保存在一個字符串變量? (這裏將會是4.4.2)

+0

您是否打擾首先進行互聯網搜索?你會發現這樣的[this](http://developer.android.com/reference/java/util/Properties.html) – MarsAtomic 2014-10-28 17:54:02

+0

@MarsAtomic我沒有看到任何標籤爲android這裏。 – 2014-10-28 19:09:42

+0

@AaronC屬性本身建議Android。 Android SDK 19 = Android v 4.4 KitKat,但我們爲了禮貌而重新標記。 – MarsAtomic 2014-10-28 19:16:13

回答

0

像通常那樣讀取文件並將每行存儲在數組列表中。

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"); 
+0

您也可以提供Android特定的代碼 - 您可以使用我在OP下面提供的鏈接輕鬆地調整代碼。擴展的信息可能會幫助其他人遇到這個問題。 – MarsAtomic 2014-10-28 19:21:40