How to calculate number of documents per hour depending on their "last modified"

406 Views Asked by At

I'm working on a tool to count archived files from another program. Therefor I'm using the DirectoryStream and filter subdirectories and some files with a simple if-clause (shown below).

For the statistics I would like to know, how many documents were created per hour on average.

I'm not very experienced in working with files and directories, but I guess there is some sort of "getLastModified", getting the Timerange from oldest to youngest and the calculate the average number of docs per hour?

1

There are 1 best solutions below

7
On BEST ANSWER

Well, files have a lastModified() method, returning the timestamp of last modification. It returns 0 if the file does not exist or an I/O error occurred. To convert a Path to a File you can use the toFile() method. With that, it will be rather easy to calculate the files/hour average:

long minTimestamp = Long.MAX_VALUE; // definitely greater than any timestamp you will ever find
long maxTimestamp = 0;
int count = 0;

try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get("DIRECTORY PATH"))) {
    for(Path path: directoryStream) {
        if (!(Files.isDirectory(path) || path.toString().endsWith("\\databaseinfo.xml") || path.toString().endsWith(".log"))) {
            long lastModified = path.toFile().lastModified();
            if (lastModified > 0L) { // check that no error occurred
                if (lastModified < minTimestamp) minTimestamp = lastModified; // new minimum
                if (maxTimestamp < lastModified) maxTimestamp = lastModified; // new maximum
            }
            count = count + 1;
        }
    }

} catch (IOException e) {
    e.printStackTrace();
}
System.out.println(count);
double filesPerHour = 0;
if (maxTimestamp != minTimestamp) { // avoid division by 0
    filesPerHour = (double) count * 60 * 60 * 1000 / (maxTimestamp - minTimestamp); // 60 * 60 * 1000 = milliseconds in one hour
}
System.out.println(filesPerHour);

Edit: Inverted the if condition, to avoid the empty if statement which had code in the else block