private String readFile(){
     try {
     System.out.println("打开文件");                        FileInputStream fileInputStream=openFileInput("/sdcard/sd.txt");//这里有错?
System.out.println("打开成功");//运行不到这里

byte[] buffer=new byte[fileInputStream.available()];
System.out.println("开始读取");
fileInputStream.read(buffer);
System.out.println("读取完成");
fileInputStream.close();
return new String(buffer,"GBK");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
    }
我的sdcard目录如下:
+data
-sdcard
 +LOST.DIR
  sd.txt
+system

解决方案 »

  1.   

    +data
    -sdcard
     +LOST.DIR
      sd.txt
    +system
    具体是怎样的???文件是放在sdcard/下的,
      

  2.   

    sdcard的位置不是绝对的. 还是通过系统获取sdcard 的位置吧 
      

  3.   

    File SDFile = android.os.Environment.getExternalStorageDirectory();
    FileInputStream fileInputStream=openFileInput(SDFile.getAbsolutePath() + File.separator + "sd.txt");冒是也不行
      

  4.   

    public FileInputStream openFileInput (String name) 
    Since: API Level 1 
    Open a private file associated with this Context's application package for reading.Parameters
    name  The name of the file to open; can not contain path separators. 
    Returns
    FileInputStream Resulting input stream.
    Throws
    FileNotFoundException   
      

  5.   

    openFileInput与openFileOutput方法只支持当前应用程序文件夹中的文件,指定路径分隔符将会导致异常。
      

  6.   

    openFileInput方法只能读取项目的私有文件,无法访问sdcard里的文件
    访问sdcard原来这样:
    FileInputStream fileInputStream=new FileInputStream("/sdcard/sd.txt");
    谢谢,各位!
      

  7.   

    File file = new File(Environment.getExternalStorageDirectory(), filename);
    FileInputStream fput = new FileInputStream(file);感觉这样好些