0

I'm trying to read a text file in config folder - MyProject/config in spring boot configuration class using MyConfig.class.getResourceAsStream(/config/test.txt).

MyProject
  -config
    -test.txt
  -src
    -main
      -java
        -com
          -myproject
            -configurations
              -MyConfig

It always return null as the resource not found. Whereas it can read the file if it is located under MyProject/src/main/resources folder.

MyProject
  -src
    -main
      -resources
        -test.txt
      -java
        -com
          -myproject
            -configurations
                -MyConfig

Is there any specific configuration required to tell spring-boot to check the entire path of the project?

@Configuration
@EnableAutoConfiguration
public class MyConfig {

  @Bean
  public Config config() { 
    return new Config(MyConfig.class.getResourceAsStream("/config/test.txt"); // This doesn't work. The same setup work if I have the file under resources folder.MyConfig.class.getResourceAsStream("/test.txt")
  }
  }
Shiny
  • 161
  • 1
  • 1
  • 9

3 Answers3

0

That method will load only from classpath/modulepath. Check of the config folder is in the classpath.

Vasco
  • 782
  • 1
  • 5
  • 22
0

Assuming you have a typicall project structure, try to put test.txt file in src/main/resources folder in order to be scanned by springboot context.

FranAB
  • 96
  • 7
0

For example<> E:\MyProject\config\test.txt

Nyamkhuu Buyanjargal
  • 621
  • 2
  • 11
  • 29