2011-03-14 14 views
0

我用的FileInputStream上课的时候,我试图从我的文件保存在SD卡读的东西,代码如下: 代码:Android的,使用的FileInputStream从SDC读取文件,报告空指针错误

filepath =“/ sdcard/myfile/testFile” FileInputStream fileIn = null;
fileIn = new FileInputStream(filepath);字节[] InBuf =新字节[1024];
fileIn.read(InBuf); Strubg fileContent = new String(InBuf);
fileIn.close();

,当我跑我的程序,机器人一直在我的最后一行汇报空指针错误:“fileIn.close”,我真的不知道原因

回答

0

我工作到昨晚深夜,最后,我让它工作,但我不确定它是否确切的原因。我只是尝试了下面的方法,然后没有更多的空指针报告来了,

try 
{ 
    InputStream fileIn = null; 
    filepath = "/sdcard/myfile/testFile"; 
    fileIn = new BufferedInputStrream(new 
    FileInputStream(filepath)); 
    byte [] InBuf = new byte[1024]; 
    fileIn.read(InBuf); 
    ... 
    ... 
    ... 
} catch(Exception e) { 
    e.printStackTrace(); 
} finally {  
    if(fileIn != null) 
    {  
     try   
     {   
      fileIn.close();   
     } catch(IOException e) {    
      e.printStackTrace();   
     } 
    } 
} 

任何方式,非常感谢您的帮助。 ^^

1

使用方法如下:

filePath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/myfile/textfile.extenstion" 

您必须缺少扩展名。

+0

对不起,我没有错过任何权限。空指针异常不能由权限丢失引起。 – 2011-03-15 00:20:57