gpt4 book ai didi

java - 仅在创建新文件时删除 .dat 文件

转载 作者:行者123 更新时间:2023-12-01 23:36:44 25 4
gpt4 key购买 nike

我陷入了一个奇怪的境地,实际上我有一份工作在一个单独的文件夹中创建文件,并且该工作每天运行,所以每天它在该文件夹中创建文件,该文件的扩展名为 .dat 及其 zip 文件。

现在假设如果今天运行作业,它将在该文件夹中创建两个文件,第二天我想要该 zip 文件前一天保留在该文件夹中,但 .dat 文件应该在创建当天的 .dat 文件之前删除,我已经编写了代码,但它没有按照我想要的方式发生,请告知如何实现这一点

File file = new File(ilePath + s); //filepath contains the location where the file will be get created and s contain the filename

for (File f : new File(mcrpFilePath).listFiles()) { // For each
// dat
// file in
// the
// directory,
// delete
// it.

if (f.isFile()
&& file.getName().toLowerCase().endsWith(".dat")) {
f.delete();
}
}
file.createNewFile();

现在请告诉我如何才能在该文件夹中保留以前的 zip 文件,但删除前一天的 .dat 文件

最佳答案

改变

file.getName().toLowerCase().endsWith(".dat")

f.getName().toLowerCase().endsWith(".dat")

关于java - 仅在创建新文件时删除 .dat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18547737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com