Where to put data or config files loaded by my Java code when web app launches in a Vaadin 14 web app driven by Maven

后端 未结 1 330
误落风尘
误落风尘 2021-01-20 15:42

In a Vaadin 14 web app project created by the \"Plain Java Servlet\" flavor of the Vaadin Starter page, there are numerous folders automatically created by the Maven POM fil

相关标签:
1条回答
  • 2021-01-20 16:39

    Location of file

    You put them in src/main/resources as it is the convention also outside of a Vaadin application.

    Vaadin adds several other resource roots for things that later end up in places in the jar, that are conventient for vaadin to find (e.g. under META-INF/resources/...).

    resources still ends up in the root of the jar or get properly "classpathed" by the build-tools and is safe to load non-classes via the classloader in your application.

    Opening file

    You can open your text file from there by calling Class::getResourceAsStream, returning an InputStream. Note the leading slash character.

    InputStream inputStream = this.getClass().getResourceAsStream( "/myfile.txt" ) ;
    
    0 讨论(0)
提交回复
热议问题