- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Android中读取中文字符的文件与文件读取相关介绍由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、如何显示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 \n1、服务条款的确认和接纳 \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.zzvips.com/article/124534.html http://blog.sina.com.cn/s/blog_933d50ba0100wq1h.html 2、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中读取中文字符的文件与文件读取相关介绍的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我在服务器上创建了一个 JSONP 函数并像这样返回一个 UTF-8 编码的 json 对象 applyLocalization({"Name":"%E5%90%8D%E5%89%8D","Age":
我正在开发一个应用程序,在该应用程序中我从API获取数据,并且正在获取这样的汉字 “u9c9cu82b1u548cu7231” 鲜花和爱 如何转换? 最佳答案 您的字符串采用转义的unicode格式。
好吧,我已经有了这个正则表达式,用于我网站上允许的名称。但是,我还希望添加名称可能使用的其他字母。有人有好的 regex 或知道如何使它更完整吗?我已经搜索了一段时间,但找不到适合我需要的内容。 这是
好吧,我已经有了这个正则表达式,用于我网站上允许的名称。但是,我还希望添加名称可能使用的其他字母。有人有好的 regex 或知道如何使它更完整吗?我已经搜索了一段时间,但找不到适合我需要的内容。 这是
本文实例讲述了Yii框架多语言站点配置方法。分享给大家供大家参考,具体如下: 这里假设我们要建立 中文/英文 切换的站点 1. 设置全局默认的语言 文件添加代码:protected/confi
我想知道如何设置编码参数,以便当我下载文本时,它“看起来”与我在网络浏览器中的页面源代码中看到的一样,例如: readLines("http://www.baidu.com/s?wd=r+projec
我计划开发一个 web 应用程序,它将使用一种新颖的方式来帮助人们学习汉字并记住它们的含义。 由于我不想/不能花费数年时间手动翻译所有中文字符,我想知道是否有(最好是开源的)数据库(任何形式)提供此功
我知道我的问题已经在这里有了解决方案。但我只想具体说明我的情况。我有一个 json 对象,其中包含非英语字符。 例如。 {“my_chinise_name”:“吉米”}。 该对象将通过 javascr
我有一个设置,其中邮件服务器(postfix)收到的电子邮件被处理,生成的电子邮件的正文(html或纯文本)和附件被解析为单独的文件并保存,为此我使用javax mail api。 当电子邮件正文为中
我的 settings.py 看起来像这样: LANGUAGES = ( ('en', _('English')), ('fr', _('French')), #Simplif
在我的图表中,x轴需要显示中文,y轴需要显示英文,但x轴显示困惑的代码。有人可以帮助我吗? self.chart.createDefaultAxes() axis_x, axis_y = self.c
使用Python3和BeautifulSoup v4 url='http://www.eurobasket2015.org/en/compID_qMRZdYCZI6EoANOrUf9le2.seaso
我的开发应用程序名称为中文。今天我从 CoreData 收到错误: CoreData: warning: Unable to load class named '゚ᆪンレ.' for enti
我正在用 java 编写一个 rss feed 解析器,在解析包含阿拉伯文/中文/日文字符的 feed 时遇到了问题。 Example feed 当我打印它们时,我只是得到一组问号“?????? ??
在我的一个Python程序(python 2.7)中,我需要处理一些汉字: 我有一个文件A.txt,它有两列:“name”和“score”,“name”列可以取一些中文字符串,score是一个1 到
我正在学习使用 eclipse 和 ADT 插件在 Android 上开发应用。 根据android SDK 文档中的这篇文章http://androidappdocs.appspot.com/res
我有这样的中文文字:“回家” - 好像是英文的“house”。 我去 google.com,在搜索中输入“回家”并得到这样的 url: http://www.google.ru/... q=%E5%9
我正在为我的应用程序添加中文支持。 我有这条线可以对英语和其他语言进行排序 NSSortDescriptor *sortByItem = [NSSortDescriptor sortDesc
我读日语,想尝试处理一些日语文本。我使用 Python 3 尝试了这个: for i in range(1,65535): print(chr(i), end='') 然后 Python 给了
我想将文本框值翻译成特定语言,如西类牙语、中文、德语等,它们都在下面的下拉列表中,我想在标签中显示文本框翻译值,但不在标签中显示翻译值。 English J
我是一名优秀的程序员,十分优秀!