如何读取assets文件夹中的txt文件
2016-05-30 13:48
447 查看
转自:http://edison-cool911.iteye.com/blog/695145
Java代码
package com.example.ReadAsset;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
public class ReadAsset extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_asset);
try {
//Return an AssetManager instance for your application's package
InputStream is = getAssets().open("index.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, "GB2312");
// Finally stick the string into the text view.
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(text);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
}
}
Java代码
package com.example.ReadAsset;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
public class ReadAsset extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_asset);
try {
//Return an AssetManager instance for your application's package
InputStream is = getAssets().open("index.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, "GB2312");
// Finally stick the string into the text view.
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(text);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
}
}
相关文章推荐
- mac相关
- 2016"百度之星" - 复赛(Astar Round3)1001 D++游戏 hdu 5712
- Hibernate 不同数据库的连接及SQL方言
- 数组引用:C++ 数组做参数 深入分析
- 网络 使用epoll 实现TCP服务器
- oracle memory_target 跟 sga_target 的区别
- C语言实例代码
- Google I/O:Android ConstraintLayout 扁平化布局入门 andstudio 2.2
- 债权转让模式
- WAL
- 关于Hibernate Could not obtain transaction-synchronized Session for current thread
- 面试题(node核心内置类库(事件,流,文件,网络等))
- Wet Shark and Bishops
- iOS中JavaScript和OC交互
- Allegro之Enhance pad Entry(增强焊盘进入约束功能)的使用
- Android真机测试 INSTALL_FAILED_INSUFFICIENT_STORAGE 解决方法[转]
- [SimHash] the Hash-based Similarity Detection Algorithm
- 苹果消息推送机制
- UIDatePicker 轮转日期选择器 时间选择器
- Android调用WebServices