我必须将文件从一个目录移动到另一个目录。
正在使用属性文件。因此源路径和目标路径存储在属性文件中。 我也有属性阅读器类。
在我的源目录中有很多文件。如果完成操作,一个文件应该移动到另一个目录。
文件大小超过 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 - 在java中移动大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27773518/