欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中讀取中文字符的文件與文件讀取相關(guān)介紹

 更新時(shí)間:2013年06月08日 16:30:47   作者:  
InputStream.available()得到字節(jié)數(shù),然后一次讀取完,用BufferedReader.readLine()行讀取再加換行符,最后用StringBuilder.append()連接成字符串,更多祥看本文
一、如何顯示assets/license.txt(中文)的內(nèi)容?
(1)方法1:InputStream.available()得到字節(jié)數(shù),然后一次讀取完。
復(fù)制代碼 代碼如下:

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)方法2:用BufferedReader.readLine()行讀取再加換行符,最后用StringBuilder.append()連接成字符串。
A.以下是先行讀取再轉(zhuǎn)碼UTF8:
復(fù)制代碼 代碼如下:

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.以下是InputStreamReader先指定以UTF8讀取文件,再進(jìn)行讀取讀取操作:
復(fù)制代碼 代碼如下:

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;
}

另外,UTF8轉(zhuǎn)碼也可以用new String(buffer, “utf-8”)。
(3)替代方法3:將license.txt內(nèi)容作為string.xml的string,如:
<stringname="license_content">用戶協(xié)議
\n \n一、服務(wù)條款的確認(rèn)和接納
\n…
</string>
需要注意的是:string里需要加\n作為換行符,原來txt里的換行符在取得string后無效。
不可取方法4:每次讀取4096字節(jié),以UTF8轉(zhuǎn)碼,最后連接字符串。因?yàn)闈h字可能被截?cái)?,?dǎo)致4096的倍數(shù)附近的中文可能出現(xiàn)亂碼。
復(fù)制代碼 代碼如下:

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.dbjr.com.cn/kf/201207/140312.html
http://blog.sina.com.cn/s/blog_933d50ba0100wq1h.html
二、Android中讀寫文件
(1) 從resource中的raw文件夾中獲取文件并讀取數(shù)據(jù)(資源文件只能讀不能寫,\res\raw\test.txt)
復(fù)制代碼 代碼如下:

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");//選擇合適的編碼,如果不調(diào)整會(huì)亂碼
in.close();
}catch(Exception e){
e.printStackTrace();
}

(2) 從asset中獲取文件并讀取數(shù)據(jù)(資源文件只能讀不能寫,\assets\test.txt)
與raw文件夾類似,只是:
InputStream is = getAssets().open(“test.txt”);
(3) 私有文件夾下的文件存?。?data/data/包名/files/test.txt)
使用openFileOutput寫文件:
復(fù)制代碼 代碼如下:

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();
}
}

使用openFileInput讀文件:
復(fù)制代碼 代碼如下:

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) sdcard目錄下的文件存?。?mnt/sdcard/)
使用FileOutputStream寫文件:
復(fù)制代碼 代碼如下:

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();
}
}

使用FileInputStream讀文件:
復(fù)制代碼 代碼如下:

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

相關(guān)文章

  • android使用handler ui線程和子線程通訊更新ui示例

    android使用handler ui線程和子線程通訊更新ui示例

    這篇文章主要介紹了android使用handler ui線程和子線程通訊更新ui的方法,大家參考使用吧
    2014-01-01
  • Android 6.0指紋識(shí)別App開發(fā)案例

    Android 6.0指紋識(shí)別App開發(fā)案例

    這篇文章主要為大家詳細(xì)介紹了Android 6.0 指紋識(shí)別App開發(fā)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android  Intent傳遞數(shù)據(jù)底層分析詳細(xì)介紹

    Android Intent傳遞數(shù)據(jù)底層分析詳細(xì)介紹

    這篇文章主要介紹了Android Intent傳遞數(shù)據(jù)底層分析詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android?Framework原理Binder驅(qū)動(dòng)源碼解析

    Android?Framework原理Binder驅(qū)動(dòng)源碼解析

    這篇文章主要為大家介紹了Android?Framework原理Binder驅(qū)動(dòng)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android中TextView顯示圓圈背景或設(shè)置圓角的方法

    Android中TextView顯示圓圈背景或設(shè)置圓角的方法

    TextView顯示文本給用戶,并允許他們選擇編輯。TextView是一個(gè)完整的文本編輯器,但是其基本類配置為不允許編輯。下面這篇文章主要給大家介紹了關(guān)于Android中TextView顯示圓圈背景或設(shè)置圓角的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • Android中Textview超鏈接實(shí)現(xiàn)方式

    Android中Textview超鏈接實(shí)現(xiàn)方式

    TextView中的超鏈接可以通過幾種方式實(shí)現(xiàn):1.Html.fromHtml,2.Spannable,3.Linkify.addLinks。下面分別進(jìn)行測(cè)試,包括修改字體樣式,下劃線樣式,點(diǎn)擊事件等,需要的朋友可以參考下
    2016-02-02
  • android仿百度福袋紅包界面

    android仿百度福袋紅包界面

    雙十一馬上到了,又進(jìn)入到搶紅包的季節(jié),本篇文章介紹了android仿百度福袋紅包界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • Android數(shù)據(jù)共享 sharedPreferences 的使用方法

    Android數(shù)據(jù)共享 sharedPreferences 的使用方法

    這篇文章主要介紹了Android數(shù)據(jù)共享 sharedPreferences 的使用方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解使用sharedpreferences,需要的朋友可以參考下
    2017-10-10
  • kotlin源碼結(jié)構(gòu)層次詳解

    kotlin源碼結(jié)構(gòu)層次詳解

    這篇文章主要為大家介紹了kotlin源碼結(jié)構(gòu)層次詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • android分享純圖片到QQ空間實(shí)現(xiàn)方式

    android分享純圖片到QQ空間實(shí)現(xiàn)方式

    今天小編就為大家分享一篇關(guān)于android分享純圖片到QQ空間實(shí)現(xiàn)方式,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-04-04

最新評(píng)論