0

I have the following code as part of a java application which I package in a jar and call via a command script which is run on a schedule. When the application is run either via command line directly the listing of files takes a few seconds - the same when run in the IDE. When the command file runs the jar when launched by Windows Task Scheduler the file listing is incredibly slow and can take up to 25 minutes to do the same task.

The command script simply calls java -jar and launches fine but something is causing a dramatic loss in performance.

File listing code:

    private static Map<String, Long> listFiles(String directoryName) {
    File directory = new File(directoryName);
    
    Map<String, Long> fileMap = new LinkedHashMap<String, Long>();
    // get all the files from a directory
    File[] fList = directory.listFiles();
    if (fList != null) {
        for (File file : fList) {
            if (file.isFile()) {
                fileMap.put(file.getAbsolutePath(), file.length());
            } else if (file.isDirectory()) {
                fileMap.putAll(listFiles(file.getAbsolutePath()));
            }
        }
    }
    return fileMap;
}
mhollander38
  • 775
  • 3
  • 11
  • 22
  • 1
    You're going to get better performance from using a `FileVisitor` than recursion, most likely. How much of the file system *are* you recursing? – g00se Jun 23 '23 at 15:22
  • Thank you, I did some further research, tested code with FileVisitor and ended up using FileStream as what tested to be the most efficient solution to my problem. – mhollander38 Jul 03 '23 at 15:08

1 Answers1

0

I solved this issue with a combination of:

  1. Increasing the priority of the scheduled task as per the answer here: https://stackoverflow.com/a/2436089/595807

  2. Changing to use java.nio.file.DirectoryStream

     private static Map<String, Long> getFileNames(Path dir) {
     Map<String, Long> fileMap = new LinkedHashMap<String, Long>();
    
     try(DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
         for (Path path : stream) {
             if(path.toFile().isDirectory()) {
                 fileMap.putAll(getFileNames(path));
             } else {
                 fileMap.put(path.toAbsolutePath().toString(), Files.size(path));
             }
         }
     } catch(IOException e) {
         e.printStackTrace();
     }
     return fileMap; }
    
mhollander38
  • 775
  • 3
  • 11
  • 22