2014-10-09 2 views
0

내용을 읽고 데이터베이스에 저장 한 후 CSV 파일을 아카이브하려고합니다. 나는CSV 파일을 한 디렉토리에서 다른 디렉토리로 이동할 수 없습니다.

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

로하고있는 중이 야 어디 파일 = "D : 400220260.INDCORP \ 바탕 화면 \ ParameterMargin20141009.csv \ \ 사용자"
및 대상 = "D : \ 사용자 \ 400220260.INDCORP \ 바탕 화면 \ 아카이브 \ ParameterMargin20141009.csv "

미리

당신은 당신의 파일 개체가
+0

일부 코드를 보겠습니다. –

+0

내 File Object에서 FileReader를 닫는 것을 잊었습니다. 그것의 일 벌금. – Gurpaldeep

답변

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(); 
     } 
    } 
} 

프로젝트에서 해당 라이브러리를 추가하는 더 쉽게 아파치 코 몬즈 IO에서 Fileutils의의 CopyFile 수 방법을 사용하지만, 따라서 당신이 가진 것이 무엇 :

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

잘 작동하는 파일을 이동해야하지만 한 위치에서 다른 위치로 파일을 복사하는 중 – Gurpaldeep

+0

file.delete()를 사용하여 첫 번째 파일을 삭제할 수 있습니다. –