简体   繁体   English

在资源(类路径)文件夹和数据库上。

[英]On the resources (classpath) folder and database.

I'm wondering if it is ok to use your resource folder as a database folder. 我想知道是否可以将您的资源文件夹用作数据库文件夹。 I have a an application that run a small semantic database. 我有一个运行小型语义数据库的应用程序。 Most of the work is done in memory but from time to time i need to commit the data in the database. 大多数工作都在内存中完成,但是我有时会需要提交数据库中的数据。 It also saves the data, for when the program will be restarted again. 它还将保存数据,以备何时重新启动程序。 I'm asking this because it sounds weird to me to have a growing Jar/bundle. 我问这个问题是因为听起来越来越奇怪的罐子/捆绑包对我来说很奇怪。 Indeed, by default sbt or maven, put your resources in your jar/bundle. 确实,默认情况下,使用sbt或maven,将您的资源放入jar / bundle中。

Can someone enlighten me a bit about how to properly use the resources folder. 有人可以启发我一些有关如何正确使用资源文件夹的问题。 Shall it be read only ? 只能是只读的吗?

AFAIK you can't overwrite files that are in .jar from code. AFAIK,您不能从代码覆盖.jar中的文件。 But you could create a file in same location to save any data (and use default data if file does not exist). 但是您可以在同一位置创建文件以保存任何数据(如果文件不存在,则使用默认数据)。

Resources should be considered read-only, as the entire application could reside in a .jar. 资源应被视为只读资源,因为整个应用程序可以驻留在.jar中。 Also some resources are cached. 还缓存了一些资源。

However you may use a database as resource for an initial database template. 但是,您可以将数据库用作初始数据库模板的资源。 This you can copy to a subdirectory of the user's home, which also makes this application multi-user / multi-tenant. 您可以将其复制到用户主目录的子目录中,这也使该应用程序成为多用户/多租户。

InputStream dbIn = SomeClassInJar.class.getResourceAsStream("/data/initial.hdb");
Path dbPath = Paths.get(System.getProperty("user.home"), ".myapp", "db.hdb");
Files.createDirectories(dbPath.getParent()); // .myapp
Files.copy(dbIn, dbPath, StandardCopyAction.REPLACE_EXISTING);

The REPLACE_EXISTING maybe not to use. REPLACE_EXISTING可能不使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM