2012-02-28 53 views
0

我正在写一个android应用程序,它必须从FTP服务器下载文件。 似乎我可以从FTP服务器读取文件没有任何问题,但是当我尝试打开它时,应用程序会引发以下错误:没有这样的文件名或目录。Android:没有这样的文件名或目录

代码:

FTPClient ftp = new FTPClient ; // The ftpclient of apache commons 
// all the code for connecting to the server 

FileOutputStream fOut = openFileOutput("ipfile.text" , Context.MODE_WORLD_WRITEABLE) ; 
    ftp.retrieveFile("ip.text" , fOut) ; 



    Log.v("As" , "Read file with succes from drivehq") ; 

    String helpStr = "ERROR" ; 
    byte[] inputBuffer = new byte [1024] ; 

    try{ 
     FileInputStream fis = new FileInputStream("/data/data/ipfile.text") ; 
     fis.read(inputBuffer) ; 
     fis.close() ; 
     helpStr = new String (inputBuffer) ; 
     Log.v("As" , "Read file from " + helpStr) ; 



    } 
    catch(Exception e) { 
     Log.v("As" ," Unable to read the ip-file " + e.getMessage()) ; 
    } 

的logcat的:

02-28 21:31:38.741: V/As(3992): Logged on with success to drivehq 
02-28 21:31:38.911: V/As(3992): Changed working directory on drivehq 
02-28 21:31:39.972: V/As(3992): Read file with succes from drivehq 
02-28 21:31:39.972: V/As(3992): Unable to read the ip-file ipfile.text (No such file or directory) 

谢谢,汤姆

回答

1

尝试关闭FileOutputStream中 “FOUT”

可能流已锁定该文件引起尽管它已被创建,但无法读取该文件。

FileOutputStream fOut = openFileOutput("ipfile.text" , Context.MODE_WORLD_WRITEABLE) ; 
ftp.retrieveFile("ip.text" , fOut) ; 
fOut.flush(); 
fOut.close(); 

此外,您应该以类似的方式来打开你的文件,你打开使用

FileInputStream fis = openFileOutput("ipfile.text"); 

这实际上可能是你读的是不工作,而不是上述原因的原因输出文件。

+0

谢谢!这确实是最后的原因:) – tb96 2012-02-28 21:12:27

+0

太棒了!不要忘记第一部分!一旦完成了输出流,你一定要关闭输出流。 openFileOutput使您可以访问应用程序的私有上下文文件。使用FileInputStream(“/ data/data/ipfile.text”)将不起作用,因为它基本上没有权限以这种方式打开文件 – dymmeh 2012-02-28 21:17:19

+0

好的,我会牢记这一点! – tb96 2012-03-03 18:08:19

相关问题