---
title: "android读取data/data/包名/file路径下的txt文件"
author: "mmdev"
date: "2013-01-24T17:15:00Z"
tags: []
slug: "android-data-data-file-txt"
draft: false
lastmod: "2013-02-18T12:14:40Z"
id: 1807022
url: "https://mmdev.iteye.com/blog/1807022"
---

文件不能太大否则会报内存溢出

**\[java]**[view plain](http://blog.csdn.net/q445697127/article/details/7519745 "view plain")[copy](http://blog.csdn.net/q445697127/article/details/7519745 "copy")

01. packageyu.bin;
02. importjava.io.FileInputStream;
03. importorg.apache.http.util.EncodingUtils;
04. importandroid.app.Activity;
05. importandroid.os.Bundle;
06. importandroid.widget.TextView;
07. publicclassReaddataPathActivityextendsActivity{
08. TextViewtextView;
09. //这个是读取data/data/包名/file路径下的文件
10. //这个目录可以用getFilesDir()方法得到
11. /\*\*Calledwhentheactivityisfirstcreated.\*/
12. @Override
13. publicvoidonCreate(BundlesavedInstanceState){
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.main);
16. textView=(TextView)findViewById(R.id.tvtext);
17. Stringtxt="";
18. try{
19. //获取文件
20. FileInputStreamfin=openFileInput("name.txt");
21. //获得长度
22. intlength=fin.available();
23. //创建字节数组
24. byte\[]buffer=newbyte\[length];
25. //读取内容
26. fin.read(buffer);
27. //获得编码格式
28. Stringtype=codetype(buffer);
29. //按编码格式获得内容
30. txt=EncodingUtils.getString(buffer,type);
31. textView.setText(txt);
32. }
33. catch(Exceptione){
34. //TODO:handleexception
35. }
36. }
37. privateStringcodetype(byte\[]head){
38. Stringtype="";
39. byte\[]codehead=newbyte\[3];
40. System.arraycopy(head,0,codehead,0,3);
41. if(codehead\[0]==-1&&codehead\[1]==-2){
42. type="UTF-16";
43. }
44. elseif(codehead\[0]==-2&&codehead\[1]==-1){
45. type="UNICODE";
46. }
47. elseif(codehead\[0]==-17&&codehead\[1]==-69&&codehead\[2]==-65){
48. type="UTF-8";
49. }
50. else{
51. type="GB2312";
52. }
53. returntype;
54. }
55. }

**分享到：**[]()[]()

文件不能太大否则会报内存溢出

**\[java]**[view plain](http://blog.csdn.net/q445697127/article/details/7519745 "view plain")[copy](http://blog.csdn.net/q445697127/article/details/7519745 "copy")

01. packageyu.bin;
02. importjava.io.FileInputStream;
03. importorg.apache.http.util.EncodingUtils;
04. importandroid.app.Activity;
05. importandroid.os.Bundle;
06. importandroid.widget.TextView;
07. publicclassReaddataPathActivityextendsActivity{
08. TextViewtextView;
09. //这个是读取data/data/包名/file路径下的文件
10. //这个目录可以用getFilesDir()方法得到
11. /\*\*Calledwhentheactivityisfirstcreated.\*/
12. @Override
13. publicvoidonCreate(BundlesavedInstanceState){
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.main);
16. textView=(TextView)findViewById(R.id.tvtext);
17. Stringtxt="";
18. try{
19. //获取文件
20. FileInputStreamfin=openFileInput("name.txt");
21. //获得长度
22. intlength=fin.available();
23. //创建字节数组
24. byte\[]buffer=newbyte\[length];
25. //读取内容
26. fin.read(buffer);
27. //获得编码格式
28. Stringtype=codetype(buffer);
29. //按编码格式获得内容
30. txt=EncodingUtils.getString(buffer,type);
31. textView.setText(txt);
32. }
33. catch(Exceptione){
34. //TODO:handleexception
35. }
36. }
37. privateStringcodetype(byte\[]head){
38. Stringtype="";
39. byte\[]codehead=newbyte\[3];
40. System.arraycopy(head,0,codehead,0,3);
41. if(codehead\[0]==-1&&codehead\[1]==-2){
42. type="UTF-16";
43. }
44. elseif(codehead\[0]==-2&&codehead\[1]==-1){
45. type="UNICODE";
46. }
47. elseif(codehead\[0]==-17&&codehead\[1]==-69&&codehead\[2]==-65){
48. type="UTF-8";
49. }
50. else{
51. type="GB2312";
52. }
53. returntype;
54. }
55. }