java - 将文件重命名为当前日期和时间 Spring Batch

标签 java spring batch-file spring-batch

我在当前正在处理的项目上使用 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/

相关文章:

java - Spring 和 Swing 应用程序一起使用吗?

spring - 使用约束冲突。无法解析包修订 com.springsource.org.apache.axis

java - Java 中组件的基线是什么

java - Mockito 测试 - 测试 write() 方法导致 NullPointerException

java - 将数据写入 MySQL 数据库时 map reduce 作业中的类转换异常

Windows 批处理文件删除早于 X 天的文件,但保留最少的 X 个文件

mysql - 如何通过批处理文件中的用户输入在mysql连接中输入用户名,密码,数据库名称

java - 即使在 100 个并发请求中,Tomcat 也会挂起

javascript - Spring Boot Web 静态资源

batch-file - 删除双空格并用一个空格替换它们