当程序执行到mycode = "UTF-8";这一行时,就直接跳到catch 里面,真是太诡异了,我明明就只是给mysode这个字符串变量赋值而已,为什么也会出异常呢?百思不得其解,而且,我读的ANSI编码的txt文件,head[0]、head[1]、head[2]的值根本不是-17、-69、-65,条件根本不满足,为什么还会执行mycode = "UTF-8"这一句呢?真的是极其诡异package com.example.tyghgy;import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;import android.util.Log;public class MyTxt { private BufferedReader br = null;
InputStream in = null;

public MyTxt(){
  File f=null;
  f= new File("/sdcard/s.txt");
  String sssss;
  
  try {
   in = new BufferedInputStream(new FileInputStream(f));
   sssss = get_code();
  } catch (FileNotFoundException e3) {
   // TODO Auto-generated catch block
   e3.printStackTrace();
  }
  
  try {
   br = new BufferedReader(new InputStreamReader(in, "utf-8"));
  } catch (UnsupportedEncodingException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  
 
}


private String get_code(){
String mycode = "";  
byte[] head = new byte[3];  
try{
        in.read(head);  
         
        if (head[0] == -1 && head[1] == -2 )  
         mycode = "UTF-16";  
        if (head[0] == -2 && head[1] == -1 )  
         mycode = "Unicode";  
        if(head[0]==-17 && head[1]==-69 && head[2] ==-65)  
         mycode = "UTF-8";  
          
        return mycode;
}catch (Exception e) {
// TODO: handle exception
    Log.v("sssssssssss", e.getMessage());
    mycode = e.getMessage();
    return mycode;
}
}
}

解决方案 »

  1.   

    http://blog.csdn.net/q445697127/article/details/7519750
    看看这个
      

  2.   

    你最好把logcat启动日志发一下看看。用Environment.getExternalStorageDirectory().getAbsolutePath()获取SDCARD的路径:String sdcardpath = Environment.getExternalStorageDirectory().getAbsolutePath();
    String fName = "s.txt";
    File f = new File(sdcardpath + File.separator + fName);
    //你代码...
    注意:还有在manifest中添加下列权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>