Warm tip: This article is reproduced from stackoverflow.com, please click
directory servlets vaadin vaadin-flow

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

发布于 2020-03-29 12:46:56

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.

screenshot of IntelliJ "Project" pane showing folders nested within project

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?

Questioner
Basil Bourque
Viewed
63
163k 2020-02-02 03:01

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