从FileOutputStream创建文件

use*_*183 2 java file-io fileoutputstream

我正在使用org.apache.commons.net.ftp远程机器下载文件.有一种方法可以将文件读取到FileOutputStream对象中.

ftpClient.retrieveFile("/" + ftpFile.getName(), fos);
Run Code Online (Sandbox Code Playgroud)

问题,这是,我有另一个接受File对象的方法.所以,我需要创建一个File目标文件FileOutputStream.我想,我需要创建一个InputStream能够从中创建文件对象FileOutputStream.它是否正确?我可能会遗漏一些东西,应该有一个简单的方法来创建File一个FileOutputStream

mcf*_*gan 5

FileOutputStream有一个构造函数,它接受一个File对象.

以下应该做你需要做的事情:

File f = new File("path/to/my/file");
if(f.createNewFile()) { // may not be necessary
    FileOutputStream fos = new FileOutputStream(f); // create a file output stream around f
    ftpClient.retrieveFile("/" + ftpFile.getName(), fos);

    otherMethod(f); // pass the file to your other method
}
Run Code Online (Sandbox Code Playgroud)