5

I am accessing a File inside the resources folder from the main class

File file = new ClassPathResource("remoteUnitsIdsInOldServer.txt").getFile();

and I am getting this error:

java.io.FileNotFoundException: class path resource [remoteUnitsIdsInOldServer.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/lopes/Documents/workspace-sts-3.9.0.RELEASE/telefonicaUtils/target/telefonicaUtils-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/remoteUnitsIdsInOldServer.txt

and I even open the jar file and the file remoteUnitsIdsInOldServer.txt is there, inside classes

en Lopes
  • 1,863
  • 11
  • 48
  • 90
  • 2
    Possible duplicate of [Reading file available in resources folder with SpringBoot](https://stackoverflow.com/questions/46563392/reading-file-available-in-resources-folder-with-springboot) – Aliaksei Stadnik Dec 14 '17 at 14:10

3 Answers3

3

The simplest solution for me was,

try {
   ClassPathResource classPathResource = new ClassPathResource("remoteUnitsIdsInOldServer.txt");
   byte[] data = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
   String content = new String(data, StandardCharsets.UTF_8);
} catch (Exception ex) {
  ex.printStackTrace();
}
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
2

It's depends on your requirements..

Case 1:

Considering you need to access text file from resource. You can simply use apache IOUtils and java ClassLoader.

Snippet (note: IOUtils package --> org.apache.commons.io.IOUtils)

    String result = "";

    ClassLoader classLoader = getClass().getClassLoader();
    try {
        result = IOUtils.toString(classLoader.getResourceAsStream("fileName"));
    } catch (IOException e) {
        e.printStackTrace();
    }

Classic way:

StringBuilder result = new StringBuilder("");

//Get file from resources folder

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("fileName").getFile());

try (Scanner scanner = new Scanner(file)) {

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        result.append(line).append("\n");
    }

    scanner.close();

} catch (IOException e) {
    e.printStackTrace();
}

Case 2:

Considering you need to access properties from resources such as xml, properties files. Its too simple, Simply use spring annotation @ImportResource({ "classpath:application-properties.xml", "classpath:context.properties" }) Hope that will be helpful to you.

Mohanraj
  • 396
  • 3
  • 8
  • 21
  • the above doesn't work in current versions of SpringBoot but `ClassPathResource` as shown in the other answer works – Alex R Mar 30 '22 at 05:36
0

Typically, a source tree would look like this:

src/
  main/
    java/
      com/
        ...
    resources/
      remoteUnitsIdsInOldServer.txt

And, using standard Maven/Gradle functionality, would produce a JAR like this:

<JAR_ROOT>/
  com/
    ...
  remoteUnitsIdsInOldServer.txt

Your listed code should work for this situation. However, you mentioned looking in your JAR "inside classes". I wouldn't think there would be a "classes" folder within the JAR.

Good luck.

Jamie Bisotti
  • 2,605
  • 19
  • 23