Search code examples
javafileoutputstream

FileOutputStream alternative for Java 1.6


I have to retrofit a piece of java code that must be compatible with Java 1.6 and I'm looking for an alternative for fileoutputstream in the following function. I am using apache.commons FTP package.

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

FTPClient ftp = null;

public FTPFetch(String host, int port, String username, String password) throws Exception
{

    ftp = new FTPClient();
    ftp.setConnectTimeout(5000);
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    int reply;
    ftp.connect(host, port);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply))
    {
        ftp.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    if (ftp.login(username, password))
    {
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalActiveMode();
    } else
    {
        disconnect();
        errorLog.fatal("Remote FTP Login Failed. Username or Password is incorrect. Please update in configuration file.");
        System.exit(1);
    }
}

 try (FileOutputStream fos = new FileOutputStream(destination))
    {
        if (this.ftp.retrieveFile(source, fos))
        {
            return true;
        } else
        {
            return false;
        }
    } catch (IOException e)
    {
        return false;
    }

Solution

  • the code does not compile in Java 1.6 because you use try-with-resources

    The try-with-resources Statement

    The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

    ...

    In this example, the resource declared in the try-with-resources statement is a BufferedReader. The declaration statement appears within parentheses immediately after the try keyword. The class BufferedReader, in Java SE 7 and later, implements the interface java.lang.AutoCloseable. Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader.readLine throwing an IOException).

    Prior to Java SE 7, you can use a finally block to ensure that a resource is closed regardless of whether the try statement completes normally or abruptly. The following example uses a finally block instead of a try-with-resources statement:

    https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

    the alternative is:

    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(destination);
      
      if(this.ftp.retrieveFile(source, fos)) {
        return true;
      } else {
        return false;
      }
    } catch (IOException e) {
      return false;
    } finally {
      if(fos != null)
        fos.close();
    }