2015-01-05 3 views
1

한 디렉토리에서 다른 디렉토리로 파일을 이동해야합니다.자바에서 큰 파일 이동

속성 파일을 사용하고 있습니다. 따라서 원본 및 대상 경로는 속성 파일에 저장됩니다. 속성 리더 클래스도 있습니다.

제 소스 디렉토리에 많은 파일이 있습니다. 하나의 파일이 완료되면 다른 디렉토리로 이동해야합니다.

파일 크기가 500MB 이상입니다.

import java.io.File; 
import java.nio.file.Files; 
import java.nio.file.StandardCopyOption; 

import static java.nio.file.StandardCopyOption.*; 


public class Main1 
{ 

    public static String primarydir=""; 
    public static String secondarydir=""; 

    public static void main(String[] argv) 
    throws Exception 
    { 

     primarydir=PropertyReader.getProperty("primarydir"); 
     System.out.println(primarydir); 

     secondarydir=PropertyReader.getProperty("secondarydir"); 

     File dir = new File(primarydir); 

     secondarydir=PropertyReader.getProperty("secondarydir"); 


     String[] children = dir.list(); 
     if (children == null) 
     { 
      System.out.println("does not exist or is not a directory"); 
     } 
     else 
     { 
      for (int i = 0; i < children.length; i++) 
      { 
       String filename = children[i]; 
       System.out.println(filename); 

       try 
       { 
        File oldFile = new File(primarydir,children[i]); 

        System.out.println("Before Moving"+oldFile.getName()); 

        if (oldFile.renameTo(new File(secondarydir+oldFile.getName()))) 
        { 
         System.out.println("The file was moved successfully to the new folder"); 
        } 
        else 
        { 
         System.out.println("The File was not moved."); 
        } 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
      System.out.println("ok"); 
     } 
    } 

} 

내 코드가 파일을 올바른 경로로 이동하지 않습니다.

이 파일은 B 드라이브에 있어야 내 재산 파일

primarydir=C:/Desktop/A 
secondarydir=D:/B 
enter code here 

입니다. 수행하는 방법? 어느 누구든지 나를 도울 수 있습니다 .. !!

+0

'FileChannel.transferTo()의 Files.move()를 사용할 수'당신이 필요로 될 수 있습니다. –

+1

당신의 디렉토리가''\ "'로 끝나야한다고 생각합니다. 이런 식으로'dir + file.getName()'은 유효한 경로를 생성합니다. –

+0

제공 한 코드에서 secondarydir이 '\'로 끝나야합니다. primarydir의 경우에는 필요가 없습니다. – robin

답변

4

변경이 : 여기에

oldFile.renameTo(new File(secondarydir+oldFile.getName())) 

는 :

oldFile.renameTo(new File(secondarydir, oldFile.getName())) 

것이 가장 그것은 플랫폼에 따라있을 수 있습니다 할 수있는 적절한 방법으로, 경로 세그먼트에 가입 문자열 연결을 사용하는 것이 아니다.

편집 : 당신은 JDK 1.7 API를 사용할 수 있습니다 경우, 대신 File.renameTo()

+0

감사합니다. 작동 중입니다. – afu

0

코드 - 자바 방법 :

/** 
* copy by transfer, use this for cross partition copy, 
* @param sFile source file, 
* @param tFile target file, 
* @throws IOException 
*/ 
public static void copyByTransfer(File sFile, File tFile) throws IOException { 
    FileInputStream fInput = new FileInputStream(sFile); 
    FileOutputStream fOutput = new FileOutputStream(tFile); 
    FileChannel fReadChannel = fInput.getChannel(); 
    FileChannel fWriteChannel = fOutput.getChannel(); 

    fReadChannel.transferTo(0, fReadChannel.size(), fWriteChannel); 

    fReadChannel.close(); 
    fWriteChannel.close(); 
    fInput.close(); 
    fOutput.close(); 
} 

방법 사용 NIO, 그것은 성능을 향상시키기 위해 사용하는 운영 체제의 부하 작업을합니다. 당신이 일식에있는 경우

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 

, 단지 ctrl + shift + o를 사용 : 여기


가져 오기 코드입니다.

+1

당신은 * import * 명령문을 포함 할 수도 있습니다. –