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

在Java中如何将文件从目录复制到另一个目录,java,Java,我使用的是jdk6 我有两个文件夹名称是Folder1和Folder2 Folder1具有以下文件 TherMap.txt TherMap1.txt TherMap2.txt 每次Folder2只有一个文件名为TherMap.txt 我想要什么 从folder1复制任何文件并粘贴到Folder2,名称为TherMap.txt。如果Folder2中已经存在TherMap.txt,则删除并粘贴该文件 因为我写了下面的代码,但它不起作用 public void FileMoving(String

我使用的是
jdk6

我有两个文件夹名称是
Folder1
Folder2

Folder1
具有以下文件

TherMap.txt

TherMap1.txt

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

我想要什么

folder1
复制任何文件并粘贴到
Folder2
,名称为
TherMap.txt
。如果
Folder2
中已经存在
TherMap.txt
,则删除并粘贴该文件

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

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);
        }
    }
}
我在
main()中调用上述函数

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

 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
    }
 }
我的代码没有显示任何错误,即使它不工作

我怎样才能解决这个问题


谢谢

使用
apachecommons

FileUtils.copyDirectory(源代码,描述)

您的代码不起作用,因为要使用ApacheComons解决方案,您必须下载ApacheComons库,该库位于以下位置:

并添加对它的引用

由于您使用的是JRE 6,因此无法使用所有NIO文件实用程序,尽管每个人都喜欢使用Apache Commons作为快速回复论坛帖子的方式,但您可能不喜欢仅为了获得一个函数而必须添加该实用程序的想法。您还可以使用使用transferFrom方法而不使用ApacheComons的代码

public static void copyFile(File sourceFile, File destFile) throws IOException {
  if (!destFile.exists()) {
    destFile.createNewFile();
  }
  FileInputStream fIn = null;
  FileOutputStream fOut = null;
  FileChannel source = null;
  FileChannel destination = null;
  try {
    fIn = new FileInputStream(sourceFile);
    source = fIn.getChannel();
    fOut = new FileOutputStream(destFile);
    destination = fOut.getChannel();
    long transfered = 0;
    long bytes = source.size();
    while (transfered < bytes) {
      transfered += destination.transferFrom(source, 0, source.size());
      destination.position(transfered);
    }
  } finally {
    if (source != null) {
      source.close();
    } else if (fIn != null) {
      fIn.close();
    }
    if (destination != null) {
      destination.close();
    } else if (fOut != null) {
      fOut.close();
    }
  }
}
参考:


您尚未下载并添加对ApacheComons的引用的可能副本。相反,您刚刚在项目中创建了一个与copyFile模式匹配的方法存根。如果您打算使用ApacheComons解决方案,您必须下载整个库并添加对它的引用。此外,您需要删除您创建的copyFile方法存根,否则您将有一个模棱两可的方法调用。我是java方面的新手,请您解释清楚。在我需要更改代码的地方,我想将一个文件从一个目录复制到另一个目录。我不想复制和粘贴目录。我也不想一次复制和粘贴所有文件。@如果有人理解这个答案,请告诉我。我正在使用
JRE 6
@urtrulyfriend:你的类路径中有Commons IO jar吗。如果此处没有下载,我将下载并解压缩文件夹,并通过
addernaljars
选项添加
commons-io-2.4.jar
。它仍然不工作。我使用了
CopyFile[]
。这是对的。
public static void copyFile(File sourceFile, File destFile) throws IOException {
  if (!destFile.exists()) {
    destFile.createNewFile();
  }
  FileInputStream fIn = null;
  FileOutputStream fOut = null;
  FileChannel source = null;
  FileChannel destination = null;
  try {
    fIn = new FileInputStream(sourceFile);
    source = fIn.getChannel();
    fOut = new FileOutputStream(destFile);
    destination = fOut.getChannel();
    long transfered = 0;
    long bytes = source.size();
    while (transfered < bytes) {
      transfered += destination.transferFrom(source, 0, source.size());
      destination.position(transfered);
    }
  } finally {
    if (source != null) {
      source.close();
    } else if (fIn != null) {
      fIn.close();
    }
    if (destination != null) {
      destination.close();
    } else if (fOut != null) {
      fOut.close();
    }
  }
}
public static void copyFile( File from, File to ) throws IOException {
    Files.copy( from.toPath(), to.toPath() );
}