文件不能太大否则会报内存溢出
- packageyu.bin;
- importjava.io.FileInputStream;
- importorg.apache.http.util.EncodingUtils;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.widget.TextView;
- publicclassReaddataPathActivityextendsActivity{
- TextViewtextView;
- //这个是读取data/data/包名/file路径下的文件
- //这个目录可以用getFilesDir()方法得到
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- textView=(TextView)findViewById(R.id.tvtext);
- Stringtxt="";
- try{
- //获取文件
- FileInputStreamfin=openFileInput("name.txt");
- //获得长度
- intlength=fin.available();
- //创建字节数组
- byte[]buffer=newbyte[length];
- //读取内容
- fin.read(buffer);
- //获得编码格式
- Stringtype=codetype(buffer);
- //按编码格式获得内容
- txt=EncodingUtils.getString(buffer,type);
- textView.setText(txt);
- }
- catch(Exceptione){
- //TODO:handleexception
- }
- }
- privateStringcodetype(byte[]head){
- Stringtype="";
- byte[]codehead=newbyte[3];
- System.arraycopy(head,0,codehead,0,3);
- if(codehead[0]==-1&&codehead[1]==-2){
- type="UTF-16";
- }
- elseif(codehead[0]==-2&&codehead[1]==-1){
- type="UNICODE";
- }
- elseif(codehead[0]==-17&&codehead[1]==-69&&codehead[2]==-65){
- type="UTF-8";
- }
- else{
- type="GB2312";
- }
- returntype;
- }
- }
- packageyu.bin;
- importjava.io.FileInputStream;
- importorg.apache.http.util.EncodingUtils;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.widget.TextView;
- publicclassReaddataPathActivityextendsActivity{
- TextViewtextView;
- //这个是读取data/data/包名/file路径下的文件
- //这个目录可以用getFilesDir()方法得到
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- textView=(TextView)findViewById(R.id.tvtext);
- Stringtxt="";
- try{
- //获取文件
- FileInputStreamfin=openFileInput("name.txt");
- //获得长度
- intlength=fin.available();
- //创建字节数组
- byte[]buffer=newbyte[length];
- //读取内容
- fin.read(buffer);
- //获得编码格式
- Stringtype=codetype(buffer);
- //按编码格式获得内容
- txt=EncodingUtils.getString(buffer,type);
- textView.setText(txt);
- }
- catch(Exceptione){
- //TODO:handleexception
- }
- }
- privateStringcodetype(byte[]head){
- Stringtype="";
- byte[]codehead=newbyte[3];
- System.arraycopy(head,0,codehead,0,3);
- if(codehead[0]==-1&&codehead[1]==-2){
- type="UTF-16";
- }
- elseif(codehead[0]==-2&&codehead[1]==-1){
- type="UNICODE";
- }
- elseif(codehead[0]==-17&&codehead[1]==-69&&codehead[2]==-65){
- type="UTF-8";
- }
- else{
- type="GB2312";
- }
- returntype;
- }
- }