简体   繁体   中英

How to differentiate properties in properties file

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.
I want to differentiate 'db connection' and 'serial=vehicleID'

Properties class provide only flat view of all provided key-value pairs, there is no way to differentiate between blocks.

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. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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