android读取data/data/包名/file路径下的txt文件

android读取data/data/包名/file路径下的txt文件

分类: android 1042人阅读 评论(0) 收藏 举报
文件不能太大否则会报内存溢出
  1. package yu.bin;  
  2.   
  3. import java.io.FileInputStream;  
  4. import org.apache.http.util.EncodingUtils;  
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.TextView;  
  8.   
  9. public class ReaddataPathActivity extends Activity {  
  10.     TextView textView;  
  11.   
  12.     // 这个是读取data/data/包名/file路径下的文件   
  13.     // 这个目录可以用getFilesDir()方法得到   
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         textView = (TextView) findViewById(R.id.tvtext);  
  20.         String txt = "";  
  21.         try {  
  22.             // 获取文件   
  23.             FileInputStream fin = openFileInput("name.txt");  
  24.             // 获得长度   
  25.             int length = fin.available();  
  26.             // 创建字节数组   
  27.             byte[] buffer = new byte[length];  
  28.             // 读取内容   
  29.             fin.read(buffer);  
  30.             // 获得编码格式   
  31.             String type = codetype(buffer);  
  32.             // 按编码格式获得内容   
  33.             txt = EncodingUtils.getString(buffer, type);  
  34.             textView.setText(txt);  
  35.         }  
  36.         catch(Exception e) {  
  37.             // TODO: handle exception   
  38.         }  
  39.     }  
  40.   
  41.     private String codetype(byte[] head) {  
  42.         String type = "";  
  43.         byte[] codehead = new byte[3];  
  44.         System.arraycopy(head, 0, codehead, 03);  
  45.         if(codehead[0] == -1 && codehead[1] == -2) {  
  46.             type = "UTF-16";  
  47.         }  
  48.         else if(codehead[0] == -2 && codehead[1] == -1) {  
  49.             type = "UNICODE";  
  50.         }  
  51.         else if(codehead[0] == -17 && codehead[1] == -69 && codehead[2] == -65) {  
  52.             type = "UTF-8";  
  53.         }  
  54.         else {  
  55.             type = "GB2312";  
  56.         }  
  57.         return type;  
  58.     }  
  59. }  
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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值