1. 程式人生 > >Android中如何獲取asset目錄下的ini檔案

Android中如何獲取asset目錄下的ini檔案

     private void getBootUrl(Context context) throws IOException {
Log.d(TAG, "getBootUrl() start");
InputStream is = null;
BufferedReader in = null;
try {
is = context.getAssets().open("ini/boot.ini");
in = new BufferedReader(new InputStreamReader(is));
} catch (IOException e1) {
e1.printStackTrace();
Log.d(TAG, e1.getMessage());
}


StringBuffer sb = new StringBuffer();
String temp = null;// 存放每行讀取的內容


int line = 1;
String[] sbStrs = new String[] {};// 以“=”為分隔符將string分割


try {
while ((temp = in.readLine()) != null) {
if (temp.contains("boot")) {
sb.append(temp.substring((temp.indexOf("t") + 2)) + " ");// 從“=”等分隔符以後擷取子串儲存,即只儲存了boot和bootIp
line++;
// 讀取下一行
while ((temp = in.readLine()) != null) {
if (temp.contains("bootIP")) {
sb.append(temp.substring((temp.indexOf("P") + 2))
+ " ");// 從“=”等分隔符以後擷取子串儲存,即只儲存了boot和bootIp
break;
} else {
line++;
}
}
break;
}
}
in.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
bootUrl = "";
Log.d(TAG, e.getMessage());
} finally {
if (in != null) {
in.close();
}
}
sbStrs = sb.toString().split(" ");
bootUrl = sbStrs[0];
bootIpUrl = sbStrs[1];
Log.d(TAG, "getBootUrl() end");
}