Where to put data or config files loaded by my Jav

2020-04-10 14:51发布

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?

1条回答
Anthone
2楼-- · 2020-04-10 15:28

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" ) ;
查看更多
登录 后发表回答