Android中读取中文字符的文件与文件读取相关介绍
发布时间:2022-02-02 07:20:53
一、如何显示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开发中使用外部应用获取SD卡状态的方法。分享给大家供大家参考,具体如下:先来看看常规获取SD卡状态的方法if (
- 在上一篇文章:Flutter进阶—实现动画效果(二)的最后,我们实现了一个控件,其中包含各种布局和状态处理控件。以及使用自定义的动画感知绘图
- 首先我们要做的就是先把IIS(Internet信息服务)打开,我用的是win8 的系统,所以这里以win8系统的操作来讲一、IIS的一些事先
- 走马灯是一种常见的效果,本文讲一下如何用 PageView 在 Flutter 里实现一个走马灯, 效果如下,当前页面的高度比其它页面高,切
- 为了解决用一个命令(宏)给方法,类,js方法添加注释,经过几天的研究.终于得到结果了。实现的效果如下:给Java中的method添加方法:/
- 本文实例讲述了 Android 7.0开发获取存储设备信息的方法。分享给大家供大家参考,具体如下:Android 7.0开发相较之前有不少改
- 1、功能需求本实例将通过c# winform实现简单的分页功能,需要的基础知识有SQL语句,c#语言基础以及c# winform的一些简单知
- 本文实例为大家分享了C#基于Sockets类实现TCP通讯的具体代码,供大家参考,具体内容如下最终效果TCPClientusing Syst
- 目录简介springfox大致原理:SpringBoot整合Swagger2引入依赖编写配置类配置SwaggerSwagger2常用注解使用
- 本文实例为大家分享了android实现选项卡功能,通过计算偏移量,设置tetxview和imageView的对应值,一些color的值读者自
- 在日常的开发中、我们都知道,Java的内存清理是通过垃圾回收器进行的,那么其是如何将没用的对象被被清理掉的呢?Java 语言的内存自动回收称
- INotifyPropertyChanged:该接口包含一个事件, 针对属性发生变更时, 执行该事件发生。// /
- 本文是Spring Security系列中的一篇。在上一篇文章中,我们通过实现UserDetailsService和UserDetails接
- 1.获取屏幕宽高方法1:int screenWidth = getWindowManager().getDefaultDisplay().g
- 文章主要涉及到以下几个问题:怎么实现Java的序列化为什么实现了java.io.Serializable接口才能被序列化transient的
- 本文实例为大家分享了java实现简易飞机大战的具体代码,供大家参考,具体内容如下整体思路1.创建游戏窗体,添加面板JPanel,重写JPan
- 1,实现效果 2,实现代码:【1】 shape_drawable.xml 
- 如果 d:\upload\file\ 文件夹不存在,会报错String strPath = "d:\\upload\\file\\
- java中的方法重载和方法重写有很多区别。 下面给出了方法重载和方法覆盖之间的差异列表:编号方法重载方法重写1方法重载用于提高程序的可读性。
- /// <summary> /// 汉字转拼音缩写 /// </summary> /// <param nam