2

I am trying the below code where i reading files and executing from the resources folder.

File[] fileList = (new File(getClass().getResource("/jsonData").toURI())).listFiles();

for (File file : fileList) {
    if (file.isFile()) {
        System.out.println(file.toString());
    }
}

Now when trying to make build in test env I am getting this error :

Caused by: java.lang.IllegalArgumentException: URI is not hierarchical

After investigation on internet I am using below code but no luck, (I need to read folder and its file.)

File[] fileList = (new File(getClass().getResourceAsStream("/geoJson").toString())).listFiles();
David
  • 4,266
  • 8
  • 34
  • 69
  • 1
    Spring!? I like [that answer](https://stackoverflow.com/a/33791922/592355) ..leading (in comment) to [PathMatchingResourcePatternResolver](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/support/PathMatchingResourcePatternResolver.html) – xerx593 Oct 01 '22 at 10:25
  • 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. Spring has some solutions like the one @xerx593 mentioned. – Rob Spoor Oct 01 '22 at 10:51
  • @xerx593 I am trying that answer but here in my case I have 4 files in `jsondata` folder which is placed in resoucres. – David Oct 01 '22 at 11:27
  • When there are only 4 files, and you know their name.. then just (4x) `@Value("classpath:/jsonData/foo.json") Resource fooResource;` ?? ...the `File` will be (if) accessible via `fooResource.getFile()`...https://www.baeldung.com/spring-classpath-file-access – xerx593 Oct 01 '22 at 12:06
  • I am using the this way where exactly I am reading the file `Object obj = parser.parse(new FileReader(filename.toString)));` – David Oct 01 '22 at 15:44

0 Answers0