簡體   English   中英

保存相對於工作目錄的文件

[英]Saving a file relative to working directory

我正在編寫一個程序,可從ftp下載文件,我希望將文件放置在工作目錄中的“文件”文件夾中,但是我不確定如何更改輸出流來執行此操作。

這是我的代碼:

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以包括目錄名稱(“文件”),例如(由於@MadProgrammer):

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

另外,如果該目錄不存在,請在開頭添加以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM