相对于工作目录保存文件

Saving a file relative to working directory

我正在编写一个从 ftp 下载文件的程序,我希望将文件放在工作目录中名为 "files" 的文件夹中,但我不确定如何更改我的输出流来执行此操作。

这是我的代码:

String remoteFile1 = f.getName();
System.out.println(remoteFile1);
File downloadFile1 = new File(fileName);
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = client.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
    System.out.println(fileName + " has been downloaded successfully.");
}

我不确定我需要编辑哪一部分来更改目录。

请尝试更改您的 fileName 以包含目录名称 ("files"),例如(感谢 @MadProgrammer):

File downloadFile1 = new File("files/", fileName); // Modify this line

另外,如果目录不存在,请在开头添加如下代码:

new File("files").mkdirs();