Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Assert.state在Spring中已弃用?_Java_Spring Boot_Spring Batch_Assert - Fatal编程技术网

Java Assert.state在Spring中已弃用?

Java Assert.state在Spring中已弃用?,java,spring-boot,spring-batch,assert,Java,Spring Boot,Spring Batch,Assert,我试图删除位于桌面/outputs/ public class FileDeleteTasklet implements Tasklet,InitializingBean { @Value("${fileName}") private String fileName; @Value("home/xxx/Desktop/outputs/") private Resource directory; @Override public Repeat

我试图删除位于
桌面/outputs/

public class FileDeleteTasklet implements Tasklet,InitializingBean {

    @Value("${fileName}")
    private String fileName;

    @Value("home/xxx/Desktop/outputs/")
    private Resource directory;


    @Override
    public RepeatStatus execute(StepContribution sc, ChunkContext cc) throws Exception {

        String file = fileName+ time()+".csv";
        try {
            File dir = directory.getFile();
            Assert.state(dir.isDirectory());

            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {

                if (files[i].getName().equalsIgnoreCase(file)) {
                    boolean rename = files[i].delete();
                    if (!rename) {
                        System.out.println("Could not delete");
                        throw new UnexpectedJobExecutionException("Could not delete file " + files[i].getPath());
                    } else {
                        System.out.println(files[i].getPath() + " is deleted!");
                        break;
                    }
                }
            }
            return RepeatStatus.FINISHED;

        } catch (Exception ex) {
            System.out.println("=========== Could not delete file " + file+ " *** failed *** due to " + ex.getMessage());
        }
        return RepeatStatus.FINISHED;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
         Assert.notNull(directory, "directory must be set");
    }

}

我在Java中意识到
Assert.state已弃用
?这就是发生错误的原因吗?

isDirectory()的文档说

当且仅当此抽象路径名表示的文件存在且为目录时,为true;否则就错了

所以我假设这个文件夹不存在

断言失败与弃用无关。
查看SpringBoot5的javadocs,它说:

不赞成。从4.3.7开始,支持状态(布尔值,字符串)


不,这不是错误发生的原因。发生错误是因为它不是一个目录,就像代码所说的那样

但是您不需要断言(它本来就不应该是断言,因为它不是代码的不变量),也不需要无意义和浪费的目录搜索。只用

boolean deleted = new File(file).delete();

假设您可以控制案例问题,您应该能够做到这一点。

只需使用非弃用函数检查代码即可
Assert.state(dir.isDirectory(),“任意消息”)
Assert.state()
不是Java语言。这是春天的一部分。很难理解为什么要遍历整个目录,只是为了删除一个您已经知道名称的文件,或者为什么删除后生成的
boolean
被称为
rename
@EJP我明白了为什么吗?该代码删除所有文件。如果您已经知道文件名,则无需浪费时间和空间列出目录。他也没有说它是“在Java中”。也很难看出断言的意义,条件何时可能变为真。这不是一个真正的不变量。@Patrick如果我添加一条消息(是),我得到
无法删除文件报告\u 2018-02-22.csv***失败***因为是

boolean deleted = new File(file).delete();