a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android中讀取中文字符的文件與文件讀取相關介紹

2020-04-11 12:13:31
字體:
來源:轉載
供稿:網友
一、如何顯示assets/license.txt(中文)的內容?
(1)方法1:InputStream.available()得到字節數,然后一次讀取完。
復制代碼 代碼如下:

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.以下是先行讀取再轉碼UTF8:
復制代碼 代碼如下:

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讀取文件,再進行讀取讀取操作:
復制代碼 代碼如下:

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轉碼也可以用new String(buffer, “utf-8”)。
(3)替代方法3:將license.txt內容作為string.xml的string,如:
<stringname="license_content">用戶協議
/n /n一、服務條款的確認和接納
/n…
</string>
需要注意的是:string里需要加/n作為換行符,原來txt里的換行符在取得string后無效。
不可取方法4:每次讀取4096字節,以UTF8轉碼,最后連接字符串。因為漢字可能被截斷,導致4096的倍數附近的中文可能出現亂碼。
復制代碼 代碼如下:

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

//www.5lwq4hdr.cn/kf/201207/140312.html
http://blog.sina.com.cn/s/blog_933d50ba0100wq1h.html
二、Android中讀寫文件
(1) 從resource中的raw文件夾中獲取文件并讀取數據(資源文件只能讀不能寫,/res/raw/test.txt)
復制代碼 代碼如下:

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

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

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讀文件:
復制代碼 代碼如下:

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寫文件:
復制代碼 代碼如下:

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讀文件:
復制代碼 代碼如下:

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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲六月丁香色婷婷综合久久 | 理论片91 | 久久综合久久综合久久综合 | 国产精品国色综合久久 | 澳门久久| 久久久久久久久久久国产精品 | 99精品国产一区二区三区 | 最新黄色网址在线播放 | 中文字幕欧美日韩 | 日韩激情视频一区 | 精品一区二区免费视频 | 欧美日韩中文字幕 | 国产偷自视频区视频 | 国产精品久久久久久久久 | 91精品国产一区二区三区蜜臀 | 日日躁夜夜操 | 欧美小电影 | 久久艹在线观看 | 日韩在线一区二区 | 欧美一区二区三区 | 成人免费看 | 欧美成人激情视频 | 亚洲特级 | 中文成人在线 | 中文字幕视频一区 | 黄色性视频 | 欧美成人精品一区二区男人看 | 在线小视频 | 欧美成人影院在线 | 草草草久久久 | 亚洲啪视频| 久久久久久久久久久久国产精品 | 国产极品美女高潮无套av个 | 国产精品久久久久久亚洲调教 | 欧美视频在线免费看 | 国产一区二区精品久久 | 成人性生交大片免费看中文带字幕 | 蜜桃精品久久久久久久免费影院 | 一区二区精品视频 | 免费日韩成人 | 久久91精品|