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,如:

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

}

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

时间: 2024-08-08 10:09:22

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

asp.net C#读取中文字符代码

asp教程.net c#读取中文字符代码 private static void fnfileprocess() { streamreader reader = new streamreader(@"d:1500.txt", encoding.getencoding("gb2312")); streamwriter writeren = new streamwriter(@"d:english.txt", false, encoding.utf8

android中Parcel中文乱码的解决:巧用十六进制

android中Parcel中文乱码的解决:巧用十六进制 把需要传递的中文转化成十六进制的字符串. str的格式: "48", "65", "6C", "6C", "6F", "CD", "FB", "BE", "A9", "D7", "E9" private String hex2Str

如何在android中使用中文分词

问题描述 如何在android中使用中文分词 大家好,我想要在android里面使用中文分词功能,要求实现分词功能的软件是开源软件,不知道有没有谁知道应该使用什么软件?谢谢了. 解决方案 http://www.chengxuyuans.com/Android/62017.html 解决方案二: android-apktool 中文使用说明android-apktool 中文使用说明

Lua判断字符串中包含中文字符的方法和计算字符串宽度函数分享_Lua

一.判断字符串中包含中文字符的方法 遍历数组,对每个字节使用string.byte(),发现有大于127的,就是汉字,可以参照下面的代码. 二.计算字符串宽度函数 复制代码 代码如下: -- 计算字符串宽度   local str = "Jimmy: 你好,世界!" local fontSize = 20 local lenInByte = #str local width = 0   for i=1,lenInByte do     local curByte = string.by

如何判断sql字段中存在中文字符?

如何判断sql字段中存在中文字符?  代码如下 复制代码 declare @t table([Name] nvarchar(10)) insert @t select '好的啊!' insert @t select '12345好的' insert @t select '123' insert @t select 'sdff' select * from @t where PATINDEX('%[吖-座]%',[name])>0 /* Name        ----------  好的啊! 1

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

一.如何显示assets/license.txt(中文)的内容? (1)方法1:InputStream.available()得到字节数,然后一次读取完. 复制代码 代码如下: private String readUserAgreementFromAsset(String assetName) { String content =""; try { InputStream is= getAssets().open(assetName); if (is != null){ DataIn

ASP连接MySQL,数据库中的中文字符在页面中显示的是“?”

问题描述 我将MySQL的编码格式全部设为了utf8,包括表.字段,但是网页中显示的中文字符为"?".(网页的编码格式也是UTF-8,文件也是.)从网页中插入中文数据到数据库,数据库中却是乱码.求教大神们,这个为问题我已经弄了一天了. 解决方案 本帖最后由 qq_27764721 于 2016-05-10 17:06:21 编辑解决方案二:自己先顶一下.会不会是类库的问题?我用的MySQLDriverCS.解决方案三:问题解决了.

Android中使用pull解析器操作xml文件的解决办法_Android

一.使用Pull解析器读取XML文件 除了可以使用SAX或DOM解析XML文件之外,大家也可以使用Android内置的Pull解析器解析XML文件. Pull解析器是一个开源的java项目,既可以用于android,也可以用于JavaEE.如果用在javaEE需要把其jar文件放入类路径中,因为Android已经集成进了Pull解析器,所以无需添加任何jar文件.android系统本身使用到的各种xml文件,其内部也是采用Pull解析器进行解析的. Pull解析器的运行方式与SAX 解析器相似.

在C#和MySQL中存取中文字符时避免乱码的方法_Mysql

当用到socket来进行网络程序开发时,大多数情况下会遇到中文字符的发送与接收,这时若对发送的字符串用默认的方式进行处理,则一般会得到一堆乱码. 由于中文字符采用双字节表示,所以对含有中文的字符串的处理一定要按UNICODE编码方式进行处理,也就是说,使用socket发送中文字串时要事先将字串转成UNICODE格式的. 下面是简单的socket通信的代码. //服务端代码 try { IPAddress MyIP = IPAddress.Parse("127.0.0.1″); TcpListen