Reputation: 21406
I am self learning Spring. I want to create a method, whose parameters will be source directory, destination directory, and file name.
boolean moveFile(String sourceDir, String targetDir, String fileName)
The method may move the file from source directory to destination directory. I have gone through spring integration file support doc. Also I have googled for examples, but always get examples where both directories at hard coded in xml file, and the source directory will be watched, and file moved to target directory when new file comes. How can I create the method I am trying to accomplish.
Thank you.
Upvotes: 1
Views: 3319
Reputation: 121282
Well, Spring Integration doesn’t provide such a functionality because it exists really as a single method in Java. See java.nio.file.Files
:
https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#move(java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...)
Move or rename a file to a target file.
Here is some samples and explanations: You can find some info here: http://tutorials.jenkov.com/java-nio/files.html
If you still looking into some Spring Integration out-of-the-box solution, the FileWritingMessageHandler
provides some functionality to copy from one file to another if the payload is a File
:
<int-file:outbound-channel-adapter id="moveFile"
directory="/destinationDir"
delete-source-files="true"
filename-generator-expression="'foo.txt'" />
This way the FileWritingMessageHandler
performs this logic:
if (!FileExistsMode.APPEND.equals(this.fileExistsMode) && this.deleteSourceFiles) {
rename(sourceFile, resultFile);
return resultFile;
}
Where rename()
is exactly this:
private static void rename(File source, File target) throws IOException {
Files.move(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
Upvotes: 3