res->new Directory踊淳,"raw"->new file,"rawinfo.txt"
activity_main.xml
android:text=" 讀取Raw文件夾中的數(shù)據(jù)"
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;
}
}
}
運行效果: