Java 1.6 的 FileOutputStream 替代方案

FileOutputStream alternative for Java 1.6

我必须改造一段必须与 Java 1.6 兼容的 java 代码,我正在寻找以下函数中 fileoutputstream 的替代方法。我正在使用 apache.commons FTP 包。

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;
    }

代码无法在 Java 1.6 中编译,因为您使用了 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

备选方案是:

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();
}

Try-with-resources (try (AutoClosable ...)) 在 Java 中不可用 6. 忽略它应该没问题,例如:

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