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

 更新时间:2013年06月08日 16:30:47   作者:  
InputStream.available()得到字节数,然后一次读取完,用BufferedReader.readLine()行读取再加换行符,最后用StringBuilder.append()连接成字符串,更多祥看本文
一、如何显示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();
}

https://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使用handler ui线程和子线程通讯更新ui示例

    android使用handler ui线程和子线程通讯更新ui示例

    这篇文章主要介绍了android使用handler ui线程和子线程通讯更新ui的方法,大家参考使用吧
    2014-01-01
  • Android 6.0指纹识别App开发案例

    Android 6.0指纹识别App开发案例

    这篇文章主要为大家详细介绍了Android 6.0 指纹识别App开发案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Android  Intent传递数据底层分析详细介绍

    Android Intent传递数据底层分析详细介绍

    这篇文章主要介绍了Android Intent传递数据底层分析详细介绍的相关资料,需要的朋友可以参考下
    2017-02-02
  • Android Framework原理Binder驱动源码解析

    Android Framework原理Binder驱动源码解析

    这篇文章主要为大家介绍了Android Framework原理Binder驱动源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android中TextView显示圆圈背景或设置圆角的方法

    Android中TextView显示圆圈背景或设置圆角的方法

    TextView显示文本给用户,并允许他们选择编辑。TextView是一个完整的文本编辑器,但是其基本类配置为不允许编辑。下面这篇文章主要给大家介绍了关于Android中TextView显示圆圈背景或设置圆角的方法,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-05-05
  • Android中Textview超链接实现方式

    Android中Textview超链接实现方式

    TextView中的超链接可以通过几种方式实现:1.Html.fromHtml,2.Spannable,3.Linkify.addLinks。下面分别进行测试,包括修改字体样式,下划线样式,点击事件等,需要的朋友可以参考下
    2016-02-02
  • android仿百度福袋红包界面

    android仿百度福袋红包界面

    双十一马上到了,又进入到抢红包的季节,本篇文章介绍了android仿百度福袋红包界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-11-11
  • Android数据共享 sharedPreferences 的使用方法

    Android数据共享 sharedPreferences 的使用方法

    这篇文章主要介绍了Android数据共享 sharedPreferences 的使用方法的相关资料,希望通过本文能帮助到大家,让大家理解使用sharedpreferences,需要的朋友可以参考下
    2017-10-10
  • kotlin源码结构层次详解

    kotlin源码结构层次详解

    这篇文章主要为大家介绍了kotlin源码结构层次详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • android分享纯图片到QQ空间实现方式

    android分享纯图片到QQ空间实现方式

    今天小编就为大家分享一篇关于android分享纯图片到QQ空间实现方式,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04

最新评论