Last week while working , I had to access some icon file which was present in my workspace. Whiel accessing it using hard coded path, I was able to access it perfectly. But, I was unable to find the relative path of that file.
Or, if you want to read file in a URL form, write it like this :
I almost wasted 1 whole day!!
Well, after wasting 1 whole day, this is what I came up with... A simple one liner... A permanent solution for this kind of problems. And, I wanted to document it somewhere, so that I won't again waste hours.
Step 1.
Simply put all the image files or other files which you want to access using relative path in a single folder. In my case, there all icons. So, I collected them all in a single folder called icons.
Step 2.
If you directly, want to read the file as a InputStream, simply use :
getClass().getResourceAsStream("/icons/closed_folder_small.png");
Or, if you want to read file in a URL form, write it like this :
getClass().getResource("/icons/closed_folder_small.png");
No comments :
Post a Comment