Andriod resource file access operation _android

Source: Internet
Author: User
Tags stringbuffer

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!

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.