android读取data/data/包名/file路径下的txt文件
2013-01-24 17:15
891 查看
文件不能太大否则会报内存溢出
[java] view
plaincopy
package yu.bin;
import java.io.FileInputStream;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ReaddataPathActivity extends Activity {
TextView textView;
// 这个是读取data/data/包名/file路径下的文件
// 这个目录可以用getFilesDir()方法得到
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.tvtext);
String txt = "";
try {
// 获取文件
FileInputStream fin = openFileInput("name.txt");
// 获得长度
int length = fin.available();
// 创建字节数组
byte[] buffer = new byte[length];
// 读取内容
fin.read(buffer);
// 获得编码格式
String type = codetype(buffer);
// 按编码格式获得内容
txt = EncodingUtils.getString(buffer, type);
textView.setText(txt);
}
catch(Exception e) {
// TODO: handle exception
}
}
private String codetype(byte[] head) {
String type = "";
byte[] codehead = new byte[3];
System.arraycopy(head, 0, codehead, 0, 3);
if(codehead[0] == -1 && codehead[1] == -2) {
type = "UTF-16";
}
else if(codehead[0] == -2 && codehead[1] == -1) {
type = "UNICODE";
}
else if(codehead[0] == -17 && codehead[1] == -69 && codehead[2] == -65) {
type = "UTF-8";
}
else {
type = "GB2312";
}
return type;
}
}
分享到:
[java] view
plaincopy
package yu.bin;
import java.io.FileInputStream;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ReaddataPathActivity extends Activity {
TextView textView;
// 这个是读取data/data/包名/file路径下的文件
// 这个目录可以用getFilesDir()方法得到
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.tvtext);
String txt = "";
try {
// 获取文件
FileInputStream fin = openFileInput("name.txt");
// 获得长度
int length = fin.available();
// 创建字节数组
byte[] buffer = new byte[length];
// 读取内容
fin.read(buffer);
// 获得编码格式
String type = codetype(buffer);
// 按编码格式获得内容
txt = EncodingUtils.getString(buffer, type);
textView.setText(txt);
}
catch(Exception e) {
// TODO: handle exception
}
}
private String codetype(byte[] head) {
String type = "";
byte[] codehead = new byte[3];
System.arraycopy(head, 0, codehead, 0, 3);
if(codehead[0] == -1 && codehead[1] == -2) {
type = "UTF-16";
}
else if(codehead[0] == -2 && codehead[1] == -1) {
type = "UNICODE";
}
else if(codehead[0] == -17 && codehead[1] == -69 && codehead[2] == -65) {
type = "UTF-8";
}
else {
type = "GB2312";
}
return type;
}
}
分享到:
相关文章推荐
- android读取data/data/包名/file路径下的txt文件
- Android中直接按路径读取properties文件
- Android从assets和raw中读取txt文件
- 在Eclipse里面怎么读取当前包下1个txt文件的路径
- java直接读取客户端txt文件 + JavaWeb中读取文件资源的路径问题
- 我的Android笔记(七)—— 读取txt文件内容
- 在Eclipse里面怎么读取当前包下1个txt文件的路径
- Android下一种打开并读取指定路径文件内容的方法
- 由TXT文件路径,读取并显示该TXT文件
- Android小知识——从SD卡中向APP中读取txt文件的时候出现乱码的问题
- 关于android中读取文件路径出现绝对路径的解决办法
- 监测程序详细介绍,快捷键的设置;txt文件里面一行行的读取、删除、添加字符串等;根据程序路径名以及程序名判断程序是否在运行;
- Android中读取一个txt文本文件,然后显示到界面上的操作
- Android 4.4 前后版本读取图库图片和拍照完美解决方案-获取文件路径
- Android 读取工程中的txt文件
- 移动开发 - Android - 读取assets目录下的txt文件(中文乱码问题)
- windows 读取一个路径下的文件夹和文件(目前只实验了txt文件)
- 在Eclipse里面怎么读取当前包下1个txt文件的路径
- Android 从txt中读取文件
- android读取txt文件中文乱码的问题