4
I did this question where doubt was how to delete files with more than 10 days of creation. I recover them from a directory and the code has already been implemented and is functional.
However, I ended up with a problem that can make the code erase all the files, in case the program goes 10 days without running. I need to save the latest file if this occurs, but as I don’t know in what order java recovers that list from windows, I can’t consider that the latest (or the first ad list) would be the latest.
How do I save the latest file by keeping the code deletion structure below? Preferably using java native resources, because I have some limitations that can cause me problems if adding external libs.
public static void removeOldFiles() {
try {
//resgato o limite de dias de um arquivo mas
//estou trabalhando com diasLimite = 10
Propriedade prop = new Propriedade();
int diasLimite = Integer.valueOf(prop.getDbBackupDelLimit());
if (diasLimite > 0) {
Date data = new Date();
Calendar c = Calendar.getInstance();
c.setTime(data);
//seta a data limite de criação
//dos backups antigos
c.add(Calendar.DATE, - diasLimite);
Date limit = c.getTime();
//pego a URL da pasta
File bkpPasta = new File(prop.getDatabaseURL() + prop.getDbBackupDir());
//listo os arquivos contidos
File[] arqs = bkpPasta.listFiles();
for (File f : arqs) {
//pego a data de ultima modificacao
//para checar se tem mais de 10 dias
Date lastModified = new Date(f.lastModified());
if (lastModified.before(limit)) {
f.delete();
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
Note: Unfortunately I cannot use resources of java 8, because the application was made on top of JDK7.
Diego, can’t create a POJO with the file and the date? If yes you can also create
Comparator
for this date-based POJO, play everything in an orderly data structure and skip the most recent element (which as per theComparator
will be in the first or last position on the list).– Anthony Accioly
@Anthonyaccioly didn’t want to increase the complexity too much. I thought of some way to compare the files, saving the most recent one in a temporary variable and deleting the oldest (similar to a vector ordering), but I’m not able to develop the idea on paper :/
– user28595
Diego, realistically, you want something trivial, without the use of libraries and without Java 8 to solve a complex problem. It’s a difficult combination... Either way you will need some sort of sorting to know if all the files are below the limit or not, as well as compare the date of the last modification. While you can scroll through the array the first time to find out which is the latest file and a second to delete all the files (potentially skipping the latest), this will end up meaning more code than the
Comparator
.– Anthony Accioly
@Anthonyaccioly for me, I would use java 8 (because the version that is installed on my machine is the 8) the problem is that these limitations were imposed on me, and as it is a simple and relatively small program, we can not argue this kind of change, Everyone who uses it would have to upgrade to Java 8 and the people here, maybe because it’s public, don’t like "updates" very much. Migrating to 7 was a cost. But you can give me an example of what it would be like with Parator?
– user28595
I have an almost ready answer using a comparator, whether that’s for you or not?
– ramaral
@ramaral is native, serves yes
– user28595