Introduction to Reading Chinese character files and file reading in Android _android

Source: Internet
Author: User
Tags readline
How do i show the content of Assets/license.txt (Chinese)?
(1) The method 1:inputstream.available () Gets the number of bytes, and then reads it out at once.
Copy Code code as follows:

private string Readuseragreementfromasset (string assetname) {
String content = "";
try {
InputStream is= getassets (). open (Assetname);
if (is!= null) {
DataInputStream DIs = Newdatainputstream (IS);
Intlength = Dis.available ();
byte[] buffer = new Byte[length];
Dis.read (buffer);
Content= encodingutils.getstring (buffer, "UTF-8");
Is.close ();
}
catch (IOException e) {
E.printstacktrace ();
}
return content;
}

(2) Method 2: Use Bufferedreader.readline () line to read and add line breaks, and finally connect to a string with Stringbuilder.append ().
A. The following is the first read and then transcoding code UTF8:
Copy Code code as follows:

private string Readuseragreementfromasset (string assetname) {
StringBuilder sb = Newstringbuilder ("");
String content = "";
try {
InputStream is= getassets (). open (Assetname);
if (is!= null) {
BufferedReader d = newbufferedreader (new InputStreamReader (IS));
while (D.ready ()) {
Sb.append (D.readline () + "\ n");
}
Content =encodingutils.getstring (sb.tostring (). GetBytes (), "UTF-8");
Is.close ();
}
catch (IOException e) {
E.printstacktrace ();
}
return content;
}

B. The following is inputstreamreader specify to read the file in UTF8 before reading read:
Copy Code code as follows:

private string Readuseragreementfromasset (string assetname) {
StringBuilder sb = Newstringbuilder ("");
String content = "";
try {
InputStream is= getassets (). open (Assetname);
if (is!= null) {
Bufferedreaderd = new BufferedReader (new InputStreamReader (IS, "UTF-8"));
while (D.ready ()) {
Sb.append (D.readline () + "\ n");
}
Content= sb.tostring ();
Is.close ();
}
catch (IOException e) {
E.printstacktrace ();
}
return content;
}

In addition, UTF8 transcoding can also be used with the new String (buffer, "utf-8").
(3) Workaround 3: License.txt content as a string of string.xml, such as:
<stringname= "license_content" > User Agreement
\ n First, the confirmation and acceptance of the terms of service
\ n..
</string>
Note that you need to add \ n as a newline character in a string, and the newline character in TXT is invalid after the string is obtained.
No method 4: Read 4096 bytes at a time, UTF8, and the last connection string. Because Chinese characters may be truncated, resulting in a multiple of 4096 of the Chinese may appear garbled.
Copy Code code as follows:

private string Readuseragreementfromasset (string assetname) {
StringBuilder sb = Newstringbuilder ("");
String content = "";
try {
InputStream is= getassets (). open (Assetname);
if (is!= null) {
DataInputStream dIs = new DataInputStream (IS);
byte[] buffer = new BYTE[1024*4];
int length = 0;
while (length = dis.read (buffer) >0) {
Content =encodingutils.getstring (buffer, 0, length, "UTF-8");
Sb.append (content);
}
Is.close ();
}
catch (IOException e) {
E.printstacktrace ();
}
return sb.tostring ();
}

Http://www.jb51.net/kf/201207/140312.html
Http://blog.sina.com.cn/s/blog_933d50ba0100wq1h.html
Second, read and write files in Android
(1) Get the file from the Raw folder in resource and read the data (resource file can only read and write, \res\raw\test.txt)
Copy Code code as follows:

String res = "";
try{
InputStream in = Getresources (). Openrawresource (R.raw.test);
int length = in.available ();
byte [] buffer = newbyte[length];
In.read (buffer);
res = encodingutils.getstring (buffer, "UTF-8");//Select the appropriate encoding, if not adjusted will garbled
In.close ();
}catch (Exception e) {
E.printstacktrace ();
}

(2) Get the file from the asset and read the data (resource file can only read and write, \assets\test.txt)
Similar to the raw folder, just:
InputStream is = Getassets (). Open ("test.txt");
(3) file access under Private folders (/data/data/package name/files/test.txt)
To write a file using Openfileoutput:
Copy Code code as follows:

public void Writefiledata (String filename,string message) {
try{
FileOutputStream fout =openfileoutput (filename,mode_private);
byte [] bytes =message.getbytes ();
Fout.write (bytes);
Fout.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}

To read a file using Openfileinput:
Copy Code code as follows:

public string Readfiledata (string fileName) {
String str = "";
try{
FileInputStream fin =openfileinput (fileName);
int length = in.available ();
byte [] bytes = Newbyte[length];
Fin.read (bytes);
str = encodingutils.getstring (bytes, "UTF-8");
Fin.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
return str;
}

(4) file access under the SDcard directory (/mnt/sdcard/)
To write a file using FileOutputStream:
Copy Code code as follows:

public void Writefile2sdcard (String filename,string message) {
try{
FileOutputStream fout = new FileOutputStream (fileName);
byte [] bytes =message.getbytes ();
Fout.write (bytes);
Fout.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}

To read a file using FileInputStream:
Copy Code code as follows:

public string Readfilefromsdcard (string fileName) {
String res= "";
try{
FileInputStream fin = Newfileinputstream (fileName);
int length =fin.available ();
byte [] buffer = newbyte[length];
Fin.read (buffer);
Res =encodingutils.getstring (buffer, "UTF-8");
Fin.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
return res;
}

Http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6017.html
Http://blog.sina.com.cn/s/blog_4d25c9870100qpax.html

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.