I have the following problem. In our integration tests we use a different config which will be loaded from test resources with the following code prior to the tests:
URL resource = ClassLoader.getSystemResource("application.conf");
This works fine as long as there are no special characters in path. For example having the following correct path
D:/Dev/projects/#FLI/flinsta/fgraph/build/resources/test/application.conf
will result in the following wrong file path given by getSystemResource
:
D:/Dev/projects/%23FLI/flinsta/fgraph/build/resources/test/application.conf
This then results in a file which simply doesn't exist. How can I make sure that something like this does not happen. Renaming the path is an option. However I would like to find a solution instead of a workaround.
Thank you for any help!
Class. getResources would retrieve the resource by the classloader which load the object. While ClassLoader. getResource would retrieve the resource using the classloader specified.
In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass().
Java programs can use two mechanisms to access resources: Applets use Applet. getCodeBase() to get the base URL for the applet code and then extend the base URL with a relative path to load the desired resource, for example with Applet. getAudioClip(url) .
To answer my own question with the help of the comments:
URL resource = ClassLoader.getSystemResource("application.conf");
String configPath = URLDecoder.decode(resource.getFile(), "UTF-8");
Variable configPath
then contains the correct path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With