Andriod resource file access operations, andriod resource files
From: http://blog.csdn.net/jianghuiquan/article/details/8569235
<? 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. extends; public class MainActivity extends AppCompatActivity {private Button btnRead = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnRead = (Button) super. findViewById (R. id. read); // read the resource file btnRead. setOnClickListener (new OnClickListener () {public void onClick (View v) {// obtain the resource object Resources res = MainActivity. this. getResources (); // use openRawResource () to read resources as R. raw. friend resource file, the result is returned to InputStream input = res. openRawResource (R. raw. friend); // read the content of the resource file. Please scan = new bytes (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 saved the file friend.txt to the res/raw folder.
Note: The raw file does not exist. You need to create it manually.