设为首页 加入收藏

TOP

Android中读取中文字符的文件与文件读取相关(一)
2014-11-24 03:20:02 来源: 作者: 【 】 浏览:5
Tags:Android 读取 中文 字符 文件 相关

一、如何显示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,如:


用户协议


\n \n一、服务条款的确认和接纳


\n…



需要注意的是: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();


}


http://www.2cto.com/kf/201207/140312.html


http://blog.sina.com.cn/s/blog_933d50ba0100wq1h.html



(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.

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 用自定义PopupWindow实现.. 下一篇Android控件倒计时的实现

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)