title: File.lastModified() alternative?
body: <p>My problem: I'm just trying to write a function that goes through a directory and gets the time that the most recent file was modified. What I mean by this is there is a directory of say ~2500 .5 megabyte files. I don't care when each file was modified, I just need the most recent modified timestamp. i.e.
  File 1 - 1392567840
  File 2 - 1392567841
  File 2403 - 1392567849
  <strong>File 7 - 1392567850</strong></p>
  
  <p>In this case, File 7 would be the most recently modified timestamp. What I have found however is using the File.lastModified() is very slow, which is also posted in another stackoverflow post, but for use with file copies.</p>
  
  <p>So for some prior research:
  <a href="https://stackoverflow.com/questions/4305448/file-lastmodified-painfully-slow">File.lastModified() painfully slow!</a></p>
  
  <p>I did search this forum and read suggestions to use multiple threads to do all of the last modified lookups, however I was wondering if there was a simpler way to do it as I just need the most recently modified value (so I think its a much simpler problem, however I'm not sure if I can avoid the individual lookups :( ).</p>
  
  <p>Some code for investigation:</p>
  
  <pre><code>public static void main(String[] args) {
      // TODO Auto-generated method stub
  
      long start;
      long end;
      long average = 0;
      int cnt = 10;
      for(int i = 0; i &lt; cnt; i++) {
          start = System.nanoTime();
          System.out.println(GetLastModifiedTimeOfFiles("./data/"));
          end = System.nanoTime();
          average += (end - start);
      }
      System.out.println("The average time it took was: " + (average/(cnt * 1.0))/1000000 + " ms to complete!");
  
  }
  
  public static long GetLastModifiedTimeOfFiles(String path) {
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles(); 
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i &lt; listOfFiles.length; i++) {
            sb.append(listOfFiles[i].getName() + " ");
        }
        System.out.println(sb.toString().length() + " chars");
  
        long time = 0;
  
        return time;
  }
  </code></pre>
  
  <p>Results:</p>
  
  <p><strong>Average of 22 ms when using .getName()</strong></p>
  
  <p><strong>Average of 631 ms when using .lastModified()</strong></p>
  
  <p>So 1) Why would .getName() perform ~30x faster than .lastModified()?</p>
  
  <p>2) Is there some alternative approach as I just need the single most recently modified file timestamp?</p>
  
  <p>3) Is there a way I could just get an array returned of all the modified timestamps for the files in the directory as opposed to individual lookups?</p>
  
  <p>Once again, I know that there was a similar post, so I hope its not a problem as I decided that post was too old from 2010. My apologies if I am in violation of anything.</p>
  
  <p>Thank you for all your help!</p>