res->new Directory,"raw"->new file,"rawinfo.txt"
activity_main.xml
android:text=" 读取Raw文件夹中的数据"
android:id="@+id/readrawbtn"
MainActivity.class
public classMainActivityextendsAppCompatActivityimplementsView.OnClickListener{
private static finalStringRAWTAG="ReadRAW";
privateButtonreadrawbtn;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
readrawbtn= (Button)findViewById(R.id.readrawbtn);
readrawbtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
caseR.id.readrawbtn:
InputStream rawis = getResources().openRawResource(R.raw.rawinfo);
InputStreamReader rawisr =null;
try{
rawisr =newInputStreamReader(rawis,"UTF-8");
BufferedReader rawbfr =newBufferedReader(rawisr);
String rawin ="";
while((rawin = rawbfr.readLine())!=null){
Log.i(RAWTAG,rawin);
} }catch(UnsupportedEncodingException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
运行效果:
网友评论