这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
InputStream.available() 得到字节数,然后一次读取完,用 BufferedReader.readLine() 行读取再加换行符,最后用 StringBuilder.append() 连接成字符串,更多祥看本文
一、如何显示 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();
- }
二、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://www.phperz.com/article/17/0714/196048.html