簡體   English   中英

用Java移動大文件

[英]Moving large files in 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驅動器中。 怎么做? 任何人都可以幫助我.. !!

更改此:

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

對此:

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

最好不要使用字符串連接來連接路徑段,因為正確的方法可能取決於平台。

編輯:如果可以使用JDK 1.7 API,則可以使用Files.move()代替File.renameTo()

代碼-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,它利用os下位操作來提高性能。


這是導入代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM