Reputation: 79
I need to replace file from one directory to another directory. I have searched in net and i found efficient solution to replace files . I am using Files.walkFileTree to traverse the directory and replacing file. Before replacing file i am checking whether file exists or not in the target directory .If file exists i need to replace the new file with same file name. But in my case my file name from source directory will be same and only the file format will be different.
I am using
Files.copy(sourcefile,targetfile,REPLACE_EXISTING);
The above code replacing file into target directory only if the filename and format is same. Otherwise it is not deleting the old file rather than placing the new fie and old file in target directory.
Original Directory
Source Folder Target Folder
a.pdf a.txt
b.pdf b.txt
After running Files.copy
Source Folder Target Folder
a.pdf a.txt
b.pdf b.txt
a.pdf
b.pdf
But i need to replace Target Folder as
Target folder
(should replace .txt file and i need only .pdf files)
a.pdf
b.pdf
Upvotes: 1
Views: 72
Reputation: 176
Your problem is two file name same and you think is a file. In java file different from extension. Maybe you need check same file name when doing.
Upvotes: 0