Android:没有这样的文件名或目录
我正在为android编写一个应用程序,它必须从FTP服务器下载一个文件。 看起来我可以从FTP服务器读取文件,没有任何问题,但是当我试图打开它时,应用程序抛出以下错误:没有这样的文件名或目录 守则:Android:没有这样的文件名或目录,android,ftp,filestream,Android,Ftp,Filestream,我正在为android编写一个应用程序,它必须从FTP服务器下载一个文件。 看起来我可以从FTP服务器读取文件,没有任何问题,但是当我试图打开它时,应用程序抛出以下错误:没有这样的文件名或目录 守则: FTPClient ftp = new FTPClient ; // The ftpclient of apache commons // all the code for connecting to the server FileOutputStream fOut = openFile
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()) ;
}
日志:
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)
谢谢,Tom尝试关闭您的文件输出流“fOut” 该流可能已锁定该文件,导致您无法读取该文件,尽管该文件已创建
FileOutputStream fOut = openFileOutput("ipfile.text" , Context.MODE_WORLD_WRITEABLE) ;
ftp.retrieveFile("ip.text" , fOut) ;
fOut.flush();
fOut.close();
此外,您应该以与使用打开输出文件类似的方式打开文件
FileInputStream fis = openFileOutput("ipfile.text");
这实际上可能是你的阅读不起作用的原因,而不是上面提到的原因 谢谢!这确实是最后一个原因:)太好了!不过别忘了第一部分!一旦完成输出流,您肯定要关闭它。openFileOutput允许您访问应用程序的私有上下文文件。使用FileInputStream(“/data/data/ipfile.text”)将无法工作,因为它基本上没有以这种方式打开文件的权限