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

    How to read files from the sub folder of resource folder.

I have some json file in resources folder like :

src 
    main
        resources 
            jsonData
                d1.json
                d2.json
                d3.json

Now I want to read this in my class which is

src 
    main
        java
            com
                myFile
                    classes 

here is what I am trying.

 File[] fileList = (new File(getClass().getResource("/jaonData").toURI())).listFiles();
    
            for (File file : listOfFiles) {
                if (file.isFile()) {
                   // my operation of Data.
                }
            }

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:

private void process() throws IOException {
    Path classBase = getClassBase();
    if (Files.isDirectory(classBase)) {
        process(classBase);
    } else {
        // classBase is the JAR file; open it as a file system
        try (FileSystem fs = FileSystems.newFileSystem(classBase, getClass().getClassLoader())) {
            Path root = fs.getPath("/");
            return loadFromBasePath(root);
        }
    }
} 

private Path getClassBase() {
    ProtectionDomain protectionDomain = getClass().getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    URL location = codeSource.getLocation();
    try {
        return Paths.get(location.toURI());
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    }
}

private void processRoot(Path root) throws IOException {
    // use root as if it's either the root of the JAR, or target/classes
    // for instance
    Path jsonData = root.resolve("jsonData");
    // Use Files.walk or Files.newDirectoryStream(jsonData)
}

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:

private Path getClassBase() {
    String resourcePath = '/' + getClass().getName().replace('.', '/') + ".class";
    URL url = getClass().getResource(resourcePath);
    String uriValue = url.toString();
    if (uriValue.endsWith('!' + resourcePath)) {
        // format: jar:<file>!<resourcePath>
        uriValue = uriValue.substring(4, uriValue.length() - resourcePath.length() - 1);
    } else {
        // format: <folder><resourcePath>
        uriValue = uriValue.substring(0, uriValue.length() - resourcePath.length());
    }
    return Paths.get(URI.create(uriValue));
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement