Nonsense not to say more, directly to everyone paste code. The specific code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical "
> <button android:id= "@+id/read" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Read resource files (Raw)"/> <textview android:id= "@+id/cont" android:layout_width= "Wrap_content" Android:
layout_height= "Wrap_content"/> </LinearLayout> package Com.example.yanlei.wifi;
Import android.content.res.Resources;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.util.Scanner; public class Mainactivity extends Appcompatactivity {private Button btnread=null; @Override protected void OnCreate (Bundl E savedinstanceState) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); btnread= (Button)
Super.findviewbyid (R.id.read); Read Resource file Btnread.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {//Get resource object Resources res=
MainActivity.this.getResources (); The resource file with R.raw.friend is read by Openrawresource (), and the result is returned to InputStream InputStream Input=res.openrawresource (r.raw.friend)
;
Read the contents of the resource file Scanner scan=new Scanner (input);
StringBuffer info=new StringBuffer ();
while (Scan.hasnext ()) Info.append (Scan.next ()). Append ("\ n");
Scan.close ();
try {input.close ();} catch (IOException e) {e.printstacktrace ();}
Toast.maketext (Getapplicationcontext (), info.tostring (), Toast.length_long). Show ();
}
}); }
}
We save the file Friend.txt to the Res/raw folder.
Note: The raw file does not exist and requires you to create it manually.
The above is a small set to introduce the Andriod resources file access to the operation of the relevant knowledge, I hope that the above help!