Reputation: 265
I'm writing an app for android which has to download a file from an FTP-server . It seems that I can read the file from the FTP-server without any problem , but when I try to open it , the app throws the following error : no such file name or directory .
The code :
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()) ;
}
The 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)
Thanks , Tom
Upvotes: 0
Views: 535
Reputation: 22306
Try closing your FileOutputStream "fOut"
Likely that stream has locked the file causing you to not be able to read the file despite it being created.
FileOutputStream fOut = openFileOutput("ipfile.text" , Context.MODE_WORLD_WRITEABLE) ;
ftp.retrieveFile("ip.text" , fOut) ;
fOut.flush();
fOut.close();
Additionally, you should be opening your file in a similar fashion you opened the output file using
FileInputStream fis = openFileOutput("ipfile.text");
This may actually be the reason your read isn't working rather than the reason above.
Upvotes: 1