I've the following code:
try{
fileOutputStream = new FileOutputStream(downloadFile1);
outputStream1 = new BufferedOutputStream(fileOutputStream);
boolean success = ftpclient.retrieveFile(completePath.get(i), outputStream1);
if (success) {
System.out.println(completePath.get(1)+" downloaded!");
}
}finally {
if (outputStream1!=null) outputStream1.close();
if(fileOutputStream!=null) fileOutputStream.close();
}
IntelliJ is giving error that FileOutputStream needs to be closed too.
whereas if I change the stream closing order in finally block
finally {
if(fileOutputStream!=null) fileOutputStream.close();
if (outputStream1!=null) outputStream1.close();
}
then there are no errors but the files are not downloaded completely as the streams was closed ahead.
Can someone suggest the right way to do this?