How to copy file from directory to another Directory in Java

java

Solution

Use `Apache Commons` FileUtils `FileUtils.copyDirectory(source, desc);`

Problem

I am using `JDK 6`. I have 2 folders names are `Folder1` and `Folder2`. `Folder1` have the following files ``` TherMap.txt TherMap1.txt TherMap2.txt ``` every time `Folder2` have only one file with name as `TherMap.txt`. What I want, copy any file from `folder1` and pasted in `Folder2` with name as `TherMap.txt`.If already `TherMap.txt` exists in `Folder2`, then delete and paste it. for I wrote the following code.but it's not working ``` 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); } } } ``` I call the above function in `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"); ``` While I am using `FileUtils` function, it showing error so I click on error, automatically new package was generated with the following code. ``` 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 } } ``` my code not showing any errors,even it's not working. How can I fix this. Thanks

Original source

Related problems