Android中读取中文字符的文件与文件读取相关介绍

Android中读取中文字符的文件与文件读取相关介绍,第1张

概述一、如何显示assets/license.txt(中文)的内容?(1)方法1:InputStream.available()得到字节数,然后一次读取完。复制代码代码如下:privateStringreadUserAgreementFromAsset(StringassetName){Stringcontent=\"\"; 一、如何显示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,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
二、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 总结

以上是内存溢出为你收集整理的Android中读取中文字符的文件与文件读取相关介绍全部内容,希望文章能够帮你解决Android中读取中文字符的文件与文件读取相关介绍所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1142542.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存