I am using JDK 6 .
I have two folder Folder1 and Folder2 .
Folder1 have the following files
TherMap.txt TherMap1.txt TherMap2.txt
every time Folder2 has only one file named TherMap.txt .
What I want,
copy any file from Folder1 and paste it into Folder2 with a name like TherMap.txt . If TherMap.txt already exists in Folder2 , delete and paste it.
because I wrote the following code. But it does not work
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 use the FileUtils function, it shows an error, so I click on the error, a new package is automatically created using the following code.
package org.apache.commons.io; import java.io.File; public class FileUtils { public static void copyFile(File sourceFilePathObject, File statusFileNameObject) {
my code does not show any errors even if it does not work.
How can I fix this.
thanks
source share