Andriod resource file access operations, andriod resource files

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.