0

I want to use File class for using a image which is in my resources folder.

new File(String.valueOf(this.getClass().getResource("Cards/Blue/1.png")));

I tried, but to console throws this error:

java.lang.IllegalArgumentException: Provided file does not exist!

Thats my resources folder:

enter image description here

Please help me. Thanks!

  • 3
    To use a resource you use getResource, or getResource as stream. You don't use a `File` class object. – matt Aug 08 '21 at 21:31
  • [ImageIO.read](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/imageio/ImageIO.html#read(java.io.InputStream)) works with either an input stream or a url. Which also happens to be what you get from 'getResource'. – matt Aug 08 '21 at 21:32
  • 2
    What's the point of the `String.valueOf` in there? Why do you expect that to work? – f1sh Aug 08 '21 at 21:33
  • 2
    Why do you need a `File` object? The purpose of that class is really to act upon paths in a file system. If you need to use an image then you need a `URL` or `InputStream` so you only need what's already at the heart of your own code – g00se Aug 08 '21 at 21:41
  • See link - you're looking for the `MyClass.class.getResource` variant, which you can directly pass to `ImageIO`, `new ImageIcon`, etc. – rzwitserloot Aug 08 '21 at 22:11

0 Answers0