android -> 读取assets中的文本文件

1, 先在 src/main/ 下新建 assets 文件夹(Directory) 并存入 db.txt 文本文件

2,

try {
    //Return an AssetManager instance for your application's package
    InputStream is = getAssets().open("db.txt");
    int size = is.available();

    // Read the entire asset into a local byte buffer.
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();

    // Convert the buffer into a string.
    String text = new String(buffer, "UTF-8");

    // Finally stick the string into the text view.
    Log.d("mft",text);
} catch (IOException e) {
    // Should never happen!
    throw new RuntimeException(e);
}

猜你喜欢

转载自mft.iteye.com/blog/2327438