'Getting exception org.apache.commons.io.FileExistsException while renaming file using Apache Commons
I need to upload a file to different folders using sftp . The data in file is same but name of the file in each folder is different . So I decided to rename the same file before sending it to different systems. Here is the code I used to export file
private void renameAndUpload(String systemName) {
String requiredfileName= myMap.get(systemName); //this map contains name of file
File oldFile= myFileStore.getMyFile();// this object contains required file
File newFile= new File(oldFile.getAbsolutePath().replace(oldFile.getName(),""+requiredfileName));
try {
FileUtils.moveFile(oldFile, newFile);
} catch (IOException e) {
e.printStackTrace();
}
myFileStore.setMyFile(newFile);
String systemExportPath=systemName+"/";
uploadFile(myFileStore.getMyFile(),systemExportPath);//export to sftp
}
I use a for loop to execute the method renameAndUpload to rename the file multiple times based on the number of systems to which the same file(with different names) needs to be sent. The logic is working fine but sometimes I am getting the below exception while using the FileUtils.moveFile(oldFile, newFile) method of apache commons
org.apache.commons.io.FileExistsException: File element in parameter 'null' already exists: 'C:\path\to_file\myfile.txt'
But whenever I am checking the sftp folders, all files are uploaded and data is present. I have no idea why this error is coming. Is there any better way to rename the same file multiple times?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
