一、如何显示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.2cto.com/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
分享到:
相关推荐
从文件读取30个字符,从文件中读取的意思是将文件中的内容显示到操作面板上,涉及到文件的打开,所以要考虑文件是否能够打开文件,不能打开就输出“can not open the file! ” 而文章中出现的while(!in.eof()) 是一...
InputStream.available()得到字节数,然后一次读取完,用BufferedReader.readLine()行读取再加换行符,最后用StringBuilder.append()连接成字符串,更多祥看本
彻底解决android读取中文txt的乱码(自动判断文档类型并转码).doc
实现了NFC读取中国银卡借记卡...如果发现卡号不对,可以看log中HexResult是否包含银行卡卡号,我只是直接截取HexResult字符串中我的银行卡卡号所在的那个位置的字符串 有什么问题可以私聊我,我有空且能解答就会回复
这个项目可以把Excel文件内容读取,然后转成自己所需的xml语言资源。大家也可以读取后做别的事情。
Android ByteArrayOutputStream() 读取TXT文件并输出,Andorid文件流方式打开文本文件的例子,在文本框中输入文件路径,点击按钮后直接读取文件内容,并输出到屏幕上显示,是一个比较基础的Java读取txt文件的例子,...
C#+读取文本文件+字符编码
如何在安卓软件中用代码实现字符串的保存与读取
android代码,简单的文件存储,文件内容的读取
DKCloudID_NFC_Android 介绍 深圳市德科物联技术有限公司的手机NFC读取身份证明文信息Demo。更多信息请访问德科官网。 如何集成到项目中 Step 1. Add the JitPack repository to your build file 打开根build....
一个关于android数据使用的小项目,本来我是用IO流的,但是android2.3很多函数对空格有编译,io流并不是varchar那样的(也可能是我不会),所以很多IO出来的字符串用到函数里都会出现差异。 被逼无奈啊。。 才用了...
1. Android中文件读写的原理: (1).所有文件的储存都是字节的储存。 (2).在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节到磁盘。 (3).在读取文件(特别是文本文件)时,也是一个字节一个...
本文实例讲述了Android开发实现读取Assets下文件及文件写入存储卡的方法。分享给大家供大家参考,具体如下: 调用一个反编译的.so文件,查看起加密和解密情况,需要解析上万的数组,而so文件加密解密都是通过Byte来...
嵌入式Android项目设计与开发 第六章 数据存储 ——基本文件存储 基本文件存储 基本文件存储就是以一种字符文件形式保存数据,数据呈现一种链式的状态,没有任何数据结构,也缺乏管理机制,但他是最基本的数据存储...
MyPdf:Android使用iText生成pdf文件并读取pdf内容
一个简单的程序,对文档进行读取和录入(字符串) 老在CSDN下东西,这次也贡献下吧,自己写的小程序,非常简易,但很容易看懂,我觉得这个是很实用的,所以放了上来。。
读取SIM 信息。 读取数据连接状态等相关信息。
很多时候我们需要将资源文件以XML形式放在assets文件夹下,我写的这个demo是一个比较通用的获取xml输入流,解析xml获取想要数据的方法,希望对大家有帮助。
android sdk中文文档 资源是Android应用程序不可或缺的部分。一般而言,资源是你想包含和引入到应用程序里面的一些外部元素,比如图片、音频、视频、文本字符串、布局、主题等。每个Android应用程序包含一个资源...