如何将文件从目录复制到Java中的另一个目录

ur *_*end 6 java

我在用JDK 6.

我有2个文件夹名称Folder1Folder2.

Folder1 有以下文件

TherMap.txt

TherMap1.txt

TherMap2.txt
Run Code Online (Sandbox Code Playgroud)

每次Folder2只有一个名称为的文件TherMap.txt.

我想要的是,

从中复制任何文件folder1并将其粘贴Folder2TherMap.txt.如果已经TherMap.txt存在Folder2,则删除并粘贴它.

因为我写了下面的代码.但它不起作用

public void FileMoving(String sourceFilePath, String destinationPath, String fileName) throws IOException {
    File destinationPathObject = new File(destinationPath);
    File sourceFilePathObject = new File(sourceFilePath);
    if ((destinationPathObject.isDirectory()) && (sourceFilePathObject.isFile()))
    //both source and destination paths are available 
    {
        //creating object for File class
        File statusFileNameObject = new File(destinationPath + "/" + fileName);
        if (statusFileNameObject.isFile())
        //Already file is exists in Destination path
        {
            //deleted File
            statusFileNameObject.delete();
            //paste file from source to Destination path with fileName as value of fileName argument
            FileUtils.copyFile(sourceFilePathObject, statusFileNameObject);
        }
        //File is not exists in Destination path.
        {
            //paste file from source to Destination path with fileName as value of fileName argument
            FileUtils.copyFile(sourceFilePathObject, statusFileNameObject);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我打电话给上面的函数 main()

 //ExternalFileExecutionsObject is class object
 ExternalFileExecutionsObject.FileMoving(
            "C:/Documents and Settings/mahesh/Desktop/InputFiles/TMapInput1.txt",
            "C:/Documents and Settings/mahesh/Desktop/Rods",
            "TMapInput.txt");
Run Code Online (Sandbox Code Playgroud)

当我使用FileUtils函数时,它显示错误,所以我点击错误,自动使用以下代码生成新包.

 package org.apache.commons.io;
 import java.io.File;
 public class FileUtils {
    public static void copyFile(File sourceFilePathObject,
        File statusFileNameObject) {
        // TODO Auto-generated method stub
    }
 }
Run Code Online (Sandbox Code Playgroud)

我的代码没有显示任何错误,即使它不起作用.

我怎样才能解决这个问题.

谢谢

Cha*_*ana 8

使用Apache Commons FileUtils FileUtils.copyDirectory(source, desc);