【问题标题】:Moving large files in java在java中移动大文件
【发布时间】:2018-03-09 00:26:02
【问题描述】:

我必须将文件从一个目录移动到另一个目录。

我正在使用属性文件。因此源和目标路径存储在属性文件中。 我也有属性阅读器类。

在我的源目录中有很多文件。如果完成操作,一个文件应该移动到另一个目录。

文件大小超过 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 盘。怎么做?任何人都可以帮助我..!!

【问题讨论】:

  • FileChannel.transferTo() 可能是您需要的。
  • 我认为您的目录应该以"\" 结尾。这样dir + file.getName() 会产生一个有效的路径。
  • 在您提供的代码中确保 secondarydir 以“\”结尾。如果是primarydir,则不需要。
  • File.rename() 在 linux 上源/目标路径位于不同分区时将不起作用,因此它可能在 Windows 上也不起作用,您需要检查 boolean 返回值以了解是否成功与否。

标签: java file


【解决方案1】:

改变这个:

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

到这里:

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

最好不要使用字符串连接来连接路径段,因为这样做的正确方法可能取决于平台。

编辑:如果您可以使用 JDK 1.7 API,您可以使用 Files.move() 而不是 File.renameTo()

【讨论】:

    【解决方案2】:

    代码 - 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;
    

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

    【讨论】:

    • 可能想要包含import 语句。
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 2011-03-23
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2021-12-02
    相关资源
    最近更新 更多