Read and Write resource files under Res/raw

Source: Internet
Author: User

To read the original format file, you must first call the getresource () function to obtain the resource object, and then call the openrawresource () function of the resource object to open the specified original format file in the form of a binary stream. After the file is read, call the close () function to close the file stream.
The core code for reading files in the original format is as follows:

Resources resources = This. getresources ();
Inputstream = NULL;
Try {
Inputstream = resources. openrawresource (R. Raw. raw_file );
Byte [] reader = new byte [inputstream. Available ()];
While (inputstream. Read (Reader )! =-1 ){
}

Self-organized:

View code

1 resources Resources = This. getresources (); // obtain the resource file when the program is initialized.
2 inputstream = NULL; // get the input stream
3 try {
4 inputstream = resources. openrawresource (R. Raw. raw_file); // open the specified original format file in the form of a binary stream
5 byte [] reader = new byte [inputstream. Available ()]; // store the obtained stream data in Reader
6 While (inputstream. Read (Reader )! =-1) {// always read to the end
7}
8 displayview. settext (new string (reader, "UTF-8"); // display in edittext
9} catch (ioexception e ){
10 log. E ("resourcefiledemo", E. getmessage (), e );
11} finally {
12 if (inputstream! = NULL ){
13 try {
14 inputstream. Close (); // close the input stream
15}
16 catch (ioexception e ){}
17}
18}

 

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.