我必須將文件從一個目錄移動到其他目錄。在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驅。怎麼做?任何人都可以幫助我.. !!
'FileChannel.transferTo()'可能是你所需要的。 –
我認爲你的目錄應該以'「\」'結尾。這樣'dir + file.getName()'會產生一個有效的路徑。 –
在您提供的代碼中,確保secondarydir以'\'結尾。 在primarydir的情況下不需要。 – robin