publicclassReadAssetActivityextendsActivity {
privateTextView mTextView;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_asset);
initViews();
readAssetsData();
listAssetsFiles("fonts");// 列出文件目录
}
privatevoidreadAssetsData() {
InputStream is =null;
try{
// 读取Assets文件夹下对应文件的输入流
is = getAssets().open("asset_test.txt");
// 获取文件输入流的总大小
intsize = is.available();
// 把整个文件流放到一个Byte[]缓冲区
byte[] buffer =newbyte[size];
is.read(buffer);
is.close();
// 将缓冲区数据转换为字符串
String text =newString(buffer);
mTextView.setText(text);
}catch(IOException e) {
e.printStackTrace();
}finally{
try{
if(is !=null) {
is.close();
}
}catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
privatevoidinitViews() {
mTextView = (TextView) findViewById(R.id.text);
}
/**
* 获取Assets下指定文件夹下文件数量 情况
*
* @description:
* @author ldm
* @date 2016-4-27 上午9:25:54
*/
privatevoidlistAssetsFiles(String filePath) {
AssetManager am = getAssets();
String[] fileName;
try{
fileName = am.list(filePath);
if(fileName.length >0) {
for(inti =0; i < fileName.length; i++) {
Log.e("ldm", String.format("在"+ filePath
+"中文件路下有:[%d] 文件数量", fileName.length));
}
}
}catch(IOException e) {
e.printStackTrace();
}
}
}