2014-10-09 164 views
0

我想在讀取其內容並將其存儲在數據庫中之後將csv文件存檔。我這樣做是無法將csv文件從一個目錄移動到另一個目錄

String filename = file.getName(); 
File destiantion = new File(getAttribute(ST_ARCHIVE_FOLDER) + "/"+ filename); 
boolean fileArchived = file.renameTo(destiantion); 

其中file = 「d:\用戶\ 400220260.INDCORP \桌面\ ParameterMargin20141009.csv」
和目標=「d:\用戶\ 400220260.INDCORP \桌面\存檔\ ParameterMargin20141009.csv」

請幫助 在此先感謝

+0

讓我們看看一些代碼 – 2014-10-09 06:59:46

+0

我忘記關閉我的文件對象中的FileReader。它的工作現在很好。 – Gurpaldeep 2014-10-10 12:26:00

回答

1

你有你的文件的對象,但現在你必須將舊文件複製到新的位置。只需重命名文件就不會做你想要的。這是可以做到這樣的:

public static void copyFile(File sourceFile, File destFile) throws IOException { 
    destFile.createNewFile(); 
    FileChannel source = null; 
    FileChannel destination = null; 

    try { 
     source = new FileInputStream(sourceFile).getChannel(); 
     destination = new FileOutputStream(destFile).getChannel(); 
     destination.transferFrom(source, 0, source.size()); 
    } 
    finally { 
     if(source != null) { 
     source.close(); 
     } 
     if(destination != null) { 
     destination.close(); 
     } 
    } 
} 

什麼會更容易使用Apache的共享IO文件實用程序的的CopyFile方法,但因此,你必須添加該庫在您的項目是:

FileUtils.copyFile(File srcFile, File destFile) 
+0

我必須移動它正在工作的文件,但它正在將文件從一個位置複製到另一個位置 – Gurpaldeep 2014-10-09 07:20:39

+0

您可以使用file.delete()刪除第一個文件, – 2014-10-09 07:23:16

相關問題