Skip to content
Advertisement

How to read files from the sub folder of resource folder with using toURI

How to read files from the sub folder of resource folder with using URI

JavaScript

I have some json file in resources folder like :

JavaScript

Now I want to read this in my class which is

JavaScript

here is what I am trying.

JavaScript

my things are working fine but the problem what I am getting is i don’t want to use toURI as it is getting failed.

Advertisement

Answer

You’re probably not using Spring Boot, so how to read folder from the resolurces files in spring boot, : Getting error while running from Jar won’t help you much.

I’ll repeat myself from a comment to that question:

Everything inside a JAR file is not a file, and cannot be accessed using File, FileInputStream, etc. There are no official mechanisms to access directories in JAR files.

Fortunately, there is a non-official way, and that uses the fact that you can open a JAR file as a separate file system.

Here’s a way that works both with file-based file systems and resources in a JAR file:

JavaScript

If you don’t like using ProtectionDomain, you can use another little trick, that makes use of the fact that every class file can be read as resource:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement