文件不能太大否则会报内存溢出
import java.io.FileInputStream;import org.apache.http.util.EncodingUtils;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class ReadAnythingPathActivity extends Activity {    TextView textView;    // 这个是读取SDCard任意路径下的文件    /** 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 {            // 文件路径            String filename = "/sdcard/ansi1.txt";                        // 或 String filename = "mnt/sdcard/ansi1.txt";            // 文件流读取文件            FileInputStream fin = new FileInputStream(filename);            // 获得字符长度            int length = fin.available();            // 创建字节数组            byte[] buffer = new byte[length];            // 把字节流读入数组中            fin.read(buffer);            // 关闭文件流            fin.close();            // 获得编码格式            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;    }}

更多相关文章

  1. Android(安卓)Studio3.2 Butter Knife配置填坑
  2. android studio的安装信息
  3. 在Android中使用NDK调用OpenGl
  4. Android_开发 Android中CookieManager的底层实现
  5. Android(安卓)Studio 多渠道打包遇到的问题总结
  6. android 使用include 调用内部组件
  7. Android2.2快速入门
  8. android adb 读写模式 挂载文件系统
  9. NPM 和webpack 的基础使用

随机推荐

  1. jni开发之一
  2. Android进阶自定义控件之滑动开关
  3. Handler消息机制的原理及应用场景
  4. Android开发学习之一——Android全景概述
  5. [Android] 图像各种处理系列文章合集
  6. 在线免费考一个Android资格认证证书吧
  7. android中传感器的应用
  8. Android 的singleLine废弃解决
  9. Android:Thread & Handler 线程 消息循环
  10. android 下载管理器