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

爷,独闯天下 提交于 2020-02-15 10:07:24

问题


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 file process.

Where is the place to put a data file or configuration file that I will load and parse when my web app launches? Under what folder do I put my files? Should I create further nested folders? And by what file path name do I find and load such a text (XML, JSON, CSV, etc.) file?


回答1:


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" ) ;


来源:https://stackoverflow.com/questions/59996594/where-to-put-data-or-config-files-loaded-by-my-java-code-when-web-app-launches-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!