我在当前正在处理的项目上使用 Spring Batch,并且能够成功地将文件移动到新文件中,但我也很难重命名该文件。这是我的配置:
<j:step id="moveProcessedFile">
<j:tasklet ref="processedFileMove" allow-start-if-complete="true" />
</j:step>
<bean id="processedFileMove" class="com.ussco.wms.batch.wmcstint.MoveFileTasklet">
<property name="targetObject">
<bean class="JdkFileHandler" />
</property>
<property name="targetMethod" value="moveFile" />
<property name="arguments">
<list>
<value>${ipfile}</value>
<value>${ipfile.folder}</value>
</list>
</property>
</bean>
这是 MoveFileTasklet。它与 MethodInvokingTaskletAdapter 几乎相同:
public class MoveFileTasklet extends AbstractMethodInvokingDelegator<Object> implements Tasklet{
private static Logger log = LoggerFactory.getLogger(MoveFileTasklet.class);
//Create method for date and time rename within Tasklet. Implement execute as well
public boolean renameFile(File fileName){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
log.info("test");
return false;
}
/**
* Following methods directly copied from MethodInvokingTaskletAdapter
*/
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
contribution.setExitStatus(mapResult(invokeDelegateMethod()));
return RepeatStatus.FINISHED;
}
protected ExitStatus mapResult(Object result) {
if (result instanceof ExitStatus) {
return (ExitStatus) result;
}
return ExitStatus.COMPLETED;
}
}
其中 JdkFileHandler 具有方法 moveFile
:
public boolean moveFile(File fileToMove, String targetDirectory) throws IOException {
return renameFile(fileToMove, new File(targetDirectory + FILE_SEPARATOR + fileToMove.getName()));
}
并重命名文件:
private boolean renameFile(File fileToRename, File renamedTargetFile) throws IOException {
if (!fileToRename.exists()) {
throw new FileNotFoundException(fileToRename + " does not exist.");
} else if (!fileToRename.isFile()) {
throw new IOException(fileToRename + " is not a file and cannot be remaned.");
} else if (renamedTargetFile.exists()) {
throw new IOException(renamedTargetFile + " already exists so the file " + fileToRename
+ " cannot be renamed to it.");
}
return fileToRename.renameTo(renamedTargetFile);
}
现在我可以将文件移动到正确的位置,但我需要将文件重命名为 fileNamemmddyyhhmmss
另外,我无法更改 JdkFileHandler 中的任何方法。我想在我指定的 MoveFileTasklet
中实现我自己的方法,或者以其他方式实现。
我知道这些信息无处不在。我不是在寻找有关如何执行此操作的代码,而是在寻找如何继续操作的代码。现在有点卡住了。有什么建议吗?
最佳答案
发布我当前的移动存档并将当前时间附加到文件名的实现。
public static void moveArchive(File file, String initDirectory, String destDirectory) {
File finalArchive;
File initialArchive = new File(initDirectory + file.getName());
finalArchive = new File(destDirectory + file.getName() + "." + getCurrentDate("yyyyMMddHHmmss"));
if (!initialArchive.renameTo(finalArchive)) {
log.error("Couldn't move the file.");
}
}
public static String getCurrentDate(String format) {
String dtStr = "";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dt1 = new Date();
dtStr = sdf.format(dt1);
return dtStr;
}
关于java - 将文件重命名为当前日期和时间 Spring Batch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729777/