Android Read Raw file

Source: Internet
Author: User

Under the Android platform, in addition to the files in the application's private folder, you can also get input stream read data from the resource files and Assets, which are placed in the application's Res/raw directory and Assets directory, respectively. These files are packaged together with other files at compile time.

It is important to note that files from resources and assets can only be read and not written, and an example of how to read information from files in resources and assets is shown below. First, the new two text files "Test1.txt" and "Test2.txt" in the Res/raw and assets directories are used for reading, structures such as.



To avoid the hassle of string transcoding, you can set the encoding format of two text files to UTF-8. There are many ways to set up the encoding format, the simpler one is to open the text file with Windows notepad, select "UTF-8" as the encoding format in the Save As dialog box, for example.



Look at the effect after the run.

Package Xiaohang.zhimeng;

Import Java.io.InputStream;
Import Org.apache.http.util.EncodingUtils;
Import android.app.Activity;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.widget.TextView;

public class Activity02 extends activity{

public static final String ENCODING = "UTF-8";
TextView TV1;
TextView TV2;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
TV1 = (TextView) Findviewbyid (R.ID.TV1);
Tv1.settextcolor (color.red);
Tv1.settextsize (15.0f);
TV2 = (TextView) Findviewbyid (R.ID.TV2);
Tv2.settextcolor (color.red);
Tv2.settextsize (15.0f);
Tv1.settext (Getfromraw ());
Tv2.settext (Getfromassets ("test2.txt"));
}

Get files and read data from the raw folder in resources
Public String Getfromraw () {
String result = "";
try {
InputStream in = Getresources (). Openrawresource (R.raw.test1);
Gets the number of bytes in the file
int lenght = in.available ();
Create a byte array
byte[] buffer = new Byte[lenght];
Read the data in a file into a byte array
In.read (buffer);
result = encodingutils.getstring (buffer, ENCODING);
} catch (Exception e) {
E.printstacktrace ();
}
return result;
}

Get the file from the Assets folder and read the data
public string Getfromassets (string fileName) {
String result = "";
try {
InputStream in = Getresources (). Getassets (). open (FileName);
Gets the number of bytes in the file
int lenght = in.available ();
Create a byte array
byte[] buffer = new Byte[lenght];
Read the data in a file into a byte array
In.read (buffer);
result = encodingutils.getstring (buffer, ENCODING);
} catch (Exception e) {
E.printstacktrace ();
}
return result;
}
}

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.