title: Java delete files of certain size
body: <p>I have a Java tool that generates 100,000+ files that I subsequently use in "Step 2" of my process.  Due to the nature of "Step 2", I don't want any files > 1MB.  Here was the code I was using to remove those files:</p>
  
  <pre><code>File[] files = root.listFiles();
  for (File file : files) {
      if (file.isFile()) {
          if (file.length &gt; 1048576) { //size of MB
              //delete the file
              file.delete();
          }
      }
  }
  </code></pre>
  
  <p>This was working fine when the number of files was relatively small.  However, with n > 100,000 the call to listFiles() is crashing my system because of memory constraints.  I know you can filter results based on file type, but is there a way to do it based on file size?  Or, is there another way that I can easily do a large batch delete based on file size that won't crash my system?  </p>
  
  <p>FWIW, I'm running RedHat 6.  </p>