简体   繁体   English

如何区分属性文件中的属性

[英]How to differentiate properties in properties file

my.properties my.properties

#db connection
mongoconnection=mongodb://user:password@host:27017/database?maxPoolSize=1


#serial=vehicleID
077C70=47368
077C71=47367
077C72=47366

Properties properties = new Properties();
  properties.load(new FileInputStream("my.properties"));

  for (Map.Entry<Object, Object> entry : properties.entrySet()) {

   String serialNumber = (String) entry.getKey();
   String vehicleId = (String) entry.getValue();
  }

In for loop I'm getting 'mongoconnection' as key and its respected value. 在for循环中,我将'mongoconnection'作为键及其受人尊敬的值。
I want to differentiate 'db connection' and 'serial=vehicleID' 我想区分'db connection''serial=vehicleID'

Properties class provide only flat view of all provided key-value pairs, there is no way to differentiate between blocks. Properties类仅提供所有提供的键值对的平面视图,无法区分块。

You could write your own parser for the file you are trying to read 您可以为要读取的文件编写自己的解析器

I think you can make a tag to set java environment like this:-Dflag=my. 我认为您可以制作一个标签来设置Java环境,例如:-Dflag = my。 and then you can read different file to get different properties by environment. 然后您可以读取不同的文件,以根据环境获得不同的属性。

You could split your properties in two files (serial.properties, connection.properties)bBecause the properties seems to not have any relation between them. 您可以将属性分为两个文件(serial.properties,connection.properties)b,因为这些属性之间似乎没有任何关系。 One file for the connection. 一个用于连接的文件。 And the other for the serials. 和其他的连续剧。 This way, you read the serial properties and do your logic, and when you need the database url you read from the other file. 这样,您可以读取串行属性并执行逻辑,并且在需要数据库URL时从另一个文件中读取。

I normally create a properties file with properties that have something in common. 我通常创建一个属性文件,这些属性具有一些共同点。 For example, translations properties files have his own properties, database connection properties too. 例如,翻译属性文件具有自己的属性,数据库连接属性也具有。 This way is easier to edit and find the properties in the future as the project grow. 随着项目的发展,将来这种方式更容易编辑和查找属性。

Hope this helps. 希望这可以帮助。

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

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