title: Spring: Check if a classpath resource exists before loading
body: <p>I have a code where I need to check if a classpath resource exists and apply some actions.</p>
  
  <pre><code>File file = ResourceUtils.getFile("classpath:my-file.json");
  if (file.exists()) {
      // do one thing
  } else {
      // do something else
  }
  </code></pre>
  
  <p><strong>Problem:</strong> <code>ResourceUtils.getFile()</code> throws <code>FileNotFoundException</code> if the resource doesn't exist. At the same time I don't want to use exceptions for code flow and I'd like to check if a resource exists.</p>
  
  <p><strong>Question:</strong> Is there any way to check if a resource exists using Spring's API?</p>
  
  <p><strong>Why I need this to be done with Spring:</strong> Because without spring I'd need to pick a correct class loader myself which is not convenient. I'd need to have a different code to make it work in unit tests.</p>