2015-01-05 25 views
1

我必須將文件從一個目錄移動到其他目錄。在java中移動大文件

正在使用屬性文件。所以源和目標路徑存儲在屬性文件中。 上午屬性讀者類也。

在我的源代碼目錄中有很多文件。如果一個文件完成操作,它應該移動到其他目錄。

文件大小超過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"); 
     } 
    } 

} 

我的代碼文件沒有移動到正確的路徑。

這是我的屬性文件

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

文件應在B驅。怎麼做?任何人都可以幫助我.. !!

+0

'FileChannel.transferTo()'可能是你所需要的。 –

+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,你可以使用Files.move()代替File.renameTo()

+0

謝謝..它正在工作 – afu

0

碼 - Java方法:

/** 
* 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; 

如果你在Eclipse中,只需使用ctrl + shift + o

+1

您*可能*想包含'import'語句。 –