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
?
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)