android读取data/data/包名/file路径下的txt文件
2012-11-07 10:46
363 查看
android读取data/data/包名/file路径下的txt文件
分类:android 2012-04-28 13:21
1042人阅读 评论(0)
收藏
举报
文件不能太大否则会报内存溢出
[java]
view plaincopyprint?
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;
}
}
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 读取工程中的txt文件
- Android 读取大文件txt
- 关于android中读取文件路径出现绝对路径的解决办法
- android 读取txt文件
- winfrom中,嵌入的资源文件路径,并读取txt文件
- android读取sdcard路径下的文件的方法
- android, 删除SD卡音乐文件。通过getContentResolver读取音乐文件,可以获取到一个完整的路径,直接new File(这括号写获取的路径).delete();
- 基于android中读取assets目录下a.txt文件并进行解析的深入分析
- android 随手记--读取sd卡txt文件
- java直接读取客户端txt文件 + JavaWeb中读取文件资源的路径问题
- android读取内存和sd卡中的txt文件
- Android 4.4 前后版本读取图库图片和拍照完美解决方案-获取文件路径
- Android读取txt文本文件
- Android 读取手机某个文件夹目录及子文件夹中所有的txt文件
- Android读取本地TXT文件
- 监测程序详细介绍,快捷键的设置;txt文件里面一行行的读取、删除、添加字符串等;根据程序路径名以及程序名判断程序是否在运行;
- Android 读取指定路径文件
- Android 从txt中读取文件
- Python读取指定路径所有.txt文件,将文件名写入.txt