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

來源:互聯網
上載者:User

標籤:android   assets   檔案讀取   

1、擷取資源的輸入資料流

    假設資源位於assets目錄下:

    Context.getAssets().open(“sample.txt”) ;


public void deepFile(Context ctxDealFile, String path) {
 try {
 String str[] = ctxDealFile.getAssets().list(path);
 if (str.length > 0) {// 如果是目錄
 File file = new File("/data/" + path);
 file.mkdirs();
 for (String string : str) {
 path = path + "/" + string;
 System.out.println("zhoulc:\t" + path);
 // textView.setText(textView.getText()+"\t"+path+"\t");
 deepFile(ctxDealFile, path);
 path = path.substring(0, path.lastIndexOf(‘/‘));
 }
 } else {// 如果是檔案
 InputStream is = ctxDealFile.getAssets().open(path);
 FileOutputStream fos = new FileOutputStream(new File("/data/"
 + path));
 byte[] buffer = new byte[1024];
 int count = 0;
 while (true) {
 count++;
 int len = is.read(buffer);
 if (len == -1) {
 break;
 }
 fos.write(buffer, 0, len);
 }
 is.close();
 fos.close();
 }
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }

2、具體例子

     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");
}

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.