如何在Erlang中加载资源文件 [英] How to load the resource file in Erlang
问题描述
new Properties()。load(Class.getResourceAsStream(/ file.properties))
所以,我有以下问题:
- 在哪里可以(必须)保存资源文件?
- 如何在运行时确定资源文件的路径
- 如何加载它(for例如
file:consult(Filename)
)
在Erlang中,属性位于* .config文件中,通常是(但不一定)在项目的根目录中。例如:
- 芝加哥老板有boss.config
- RabbitMQ有rabbitmq.config
- Zotonic对于存储在priv / sitename / config中的不同网站有不同的配置
您可以提供配置文件通过运行
erl -config myconfig
警告:该文件应命名为myconfig.config,您应该省略扩展名。
配置文件应该被构造为方式:
[{Application1,[{Par11,Val11},...]},
... ,
{ApplicationN,[{ParN1,ValN1},...]}]。
例如:
[{kernel,[
{my_key,value}
]}]。
在erlang shell中,您可以键入:
应用程序:get_env(kernel,my_key)。
{ok,value}
我使用内核应用程序,因为它总是加载应用程序:get_env / 2返回未定义,如果应用程序未加载。你应该把任何配置放在你自己的应用程序中,并确保在调用get_env / 2之前加载它。
另外,配置是分层的,你可以把默认值* .app文件,该用户通常不需要修改。您可以在配置文件中覆盖它们,最后,您可以在命令行中提供键值对(它们将覆盖配置文件中的内容)。
您可以在这里阅读更多关于配置的信息:
http://www.erlang.org/doc/design_principles/applications.html#id74398
您还可以使配置文件更加用户友好使用注释,例如:
https://github.com/ChicagoBoss/ChicagoBoss/blob/master/skel/boss.config
What is a usual way of storing and loading resource file in Erlang. I need to create a certain human-readable dictionary and load it at application initialization. For example, in Java I would put the data in a .property file, then put it somewhere in the classpath and finally load it with help of code like this:
new Properties().load(Class.getResourceAsStream("/file.properties"))
So, I have the following questions:
- where I can (must) keep the resource file?
- how to determine in runtime the path to the resource file
- how to load it (for example
file:consult(Filename)
)
In Erlang properties are in *.config file, that usually is (but doesn't have to be) in the root directory of your project. For example:
- Chicago Boss has boss.config
- RabbitMQ has rabbitmq.config
- Zotonic has different configs for different sites stored in priv/sitename/config
You can provide config file by running
erl -config myconfig
WARNING: the file should be named "myconfig.config" and you should omit the extension.
The config file should be structured this way:
[{Application1, [{Par11,Val11},...]},
...,
{ApplicationN, [{ParN1,ValN1},...]}].
for example:
[{kernel, [
{my_key, "value"}
]}].
Than in erlang shell, you can type:
application:get_env(kernel, my_key).
{ok,"value"}
I used kernel application, because it is always loaded and application:get_env/2 returns undefined, if the application is not loaded. You should put any configs in your own application and make sure, that it is loaded before invoking get_env/2.
Also, configs are hierarchical, you can put the defaults in *.app file, that user usually doesn't have to modify. You can overwrite them in config file and finally, you can provide the key value pairs in command line (they will overwrite things, that are in config file).
You can read more about configuration here:
http://www.erlang.org/doc/design_principles/applications.html#id74398
You can also make config file more user friendly by using comments, example:
https://github.com/ChicagoBoss/ChicagoBoss/blob/master/skel/boss.config
这篇关于如何在Erlang中加载资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!