2015-11-30 25 views
1

我正在寫一個程序從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."); 
} 

我不知道我需要編輯更改目錄哪一部分。

回答

0

請嘗試更改您fileName包括目錄名(「文件」),例如(感謝@MadProgrammer):

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

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

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

或'文件(「文件」,文件名)',它有時會比較有用;) – MadProgrammer

+1

我沒有嘗試,但我得到的錯誤java.io.FileNotFoundException:文件/ Tul1D_diGly-K_r1.raw(沒有這樣的文件或目錄) – user2320239

+0

@ MadProgrammer酷,感謝這個優雅的解決方案! –