最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android API编程之Assets文件操作示例
时间:2022-06-25 23:23:13 编辑:袖梨 来源:一聚教程网
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();
}
}
}
相关文章
- 《Pokemon TCG》Mega Evolution - Chaos Rising 系列 Crobat 全图卡牌情报公开 04-25
- TGA年度最佳《光与影 33号远征队》销量爆炸 官宣突破八百万 04-25
- 刺客信条 黑旗记忆重置版现价格异常,豪华版短时直降10美元引热议 04-25
- 全本小说账号怎么注销 04-25
- 异环新手实用开荒建议指南 04-25
- 怎么解锁《Kiln》中的所有成就 04-25