Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access images from a resource folder in intelliJ IDEA without having to give entire path name

How would I do something like this in intelliJ IDEA

File file = new File("C:\Users\Account\Documents\ProjectFolder\ResourceFolder\image");

But not have to enter the whole file path in. I'm pretty sure there is a way, and I'm fairly certain it has something to do with adding it to my build path. I know how to do that in Eclipse but not in intelliJ.
Thanks in advance!

like image 654
Hobbs2000 Avatar asked Oct 24 '15 03:10

Hobbs2000


People also ask

How do I access files in resources folder in IntelliJ?

To do this, open the File > Project Structure dialog, select the Modules tab, and click on the resources folder. The result will be as shown below. Your application can now access resources from the resources folder.

How do I import an image into IntelliJ?

Right-click the pasted image in the Project tool window and select Copy | Path From Source Root. In the class in which you want to use the image, place the caret at the necessary line and press Ctrl+V to paste the path to the image. Run the class to make sure that the image is inserted correctly.

How do I specify a file path in IntelliJ?

If you just want to copy the file path you can use Ctrl + Shift + C . You can also search for action with (Ctrl + Shift + A) (for mac Command + Shift + A) and then it will show you the Keyboard shortcut for particular action. If you want to change the Keymap you can open setting page and search for Keymap.

How do I view the resources folder in a jar file?

This works when running inside and outside of a Jar file. PathMatchingResourcePatternResolver r = new PathMatchingResourcePatternResolver(); Resource[] resources = r. getResources("/myfolder/*"); Then you can access the data using getInputStream and the filename from getFilename .


Video Answer


1 Answers

You should use Java resource loading system. This is not depending on your IDE. Once you have marked a folder as resource folder in IntelliJ, it is available in your code as via resource loader.

like image 198
uwolfer Avatar answered Oct 05 '22 18:10

uwolfer