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.
Related
is it possible to achieve below scenario using properties file in Java. Thanks a lot for any feedback.
Assume I have a settings.properties file which includes,
my.name=${name}
his.name=hisNameIs${name}
In my code,
InputStream input = new FileInputStream("path/settings.properties");
Properties prop = new Properties();
prop.setProperty("my.name", "John");
prop.load(input);
String output= prop.getProperty(his.name);
System.out.println(output);
Expected Results:
hisNameIsJohn
The Apache Commons Configuration Project has an implementation capable of doing variable interpolation.
Read the section named Variable Interpolation
application.name = Killer App
application.version = 1.6.2
application.title = ${application.name} ${application.version}
You would need this third-party library in your class path, but on the other hand you will not have to worry about writing yet another implementation for this :)
You might like to read the Properties How To as well.
Posting the solution using org.apache.commons.configuration.PropertiesConfiguration
PropertiesConfiguration prop = new PropertiesConfiguration(new File("path/settings.properties"));
prop.addProperty("name", "John");
prop.getProperty(his.name);
In, "settings.properties" file.
his.name=hisNameIs${name}
I'm storing user data (nothing important, just user ID and xp amount/level) in a properties file. When using prop.store(output, comment) it resets the file completely and then stores the added data. How can I add data without it resetting the entire file?
Each time you use the store() method, it removes the old data and writes the new data.
To prevent this from happening, you must first load your data and then restore it with new data that you want to add.
This code snippet may help you:
Properties prop = new Properties();
prop.load(new FileInputStream(YOUR_PROPERTIE_FILE));
prop.setProperty(key, value);
prop.store(new FileOutputStream(YOUR_PROPERTIE_FILE), COMMENT);
I am having trouble finding out the right way to load a properties file.
The structure is : Inside src/com.training , I have my class and the properties file as well. I have to read it using the absolute path as shown in the code below to get it to work:
Properties prop = new Properties();
InputStream input = null;
input = new FileInputStream("D:/Dev/workspace/Training/src/com/training/consolemessages.properties");
prop.load(input);
System.out.print(prop.getProperty("INITIAL_MESSAGE"));
How can I use the relative path to work in this code for accessing the properties file. The properties file and the class which is accessing are both at the same level '/src/com.training'
You could put your properties files into src/resources folder, and fetch them on classpath.
You could do something like this:
ResourceBundle bundle = ResourceBundle.getBundle("resources/consolemessages");
System.out.println(bundle.getString("INITIAL_MESSAGE"));
When using ResourceBundle, Locale support is also easy to implement, should you need to have language specific properties.
Does someone knows a way to export a hazelcast config to a file?
I know for importing it, there are following ways:
hazelcast.config system property
hazelcast.xml file in the working directory
hazelcast.xml on the classpath
hazelcast-default.xml that comes with hazelcast.jar
But what can I do, if I want to save the actual config as xml. Perhaps for backup purposes. How can I do this?
I'm not aware of a configuration exporter but there is getter methods on Hazaelcast configuration class com.hazelcast.config.Config. You can use them to extract the configuration for you maps, lists, multimaps, groups, etc. For instance:
Map<String, ListConfig> listConfigs = config.getListConfigs();
for(ListConfig listConfig = listConfigs.values()) {
// export the configuration to an output file
System.out.println("List: " + listConfig.getName()+" has max size: "+listConfig.getMaxSize());
}
This question already has answers here:
How to use Java property files?
(17 answers)
Java - Properties: Add new keys to properties file in run time?
(1 answer)
Closed 8 years ago.
I have a property file having different data. I need to update the value of a property. How can achieve this using Java?
My property file contains:
# Module Password for Installer
MODULE_PASSWORD=Mg==
# Modules Selected for Installer
ausphur=yes
einfuhr=no
edec=no
emcs=no
ncts=no
suma=no
eas=no
zollager=no
I need to change yes or no values.
Create a FileOutputStream to the properties file and modify the property by using Properties.setProperty(PROPERTY_NAME,PROPERTY_VALUE) and then call store(out,null) on Properties instance
FileOutputStream out = new FileOutputStream("config.properties");
props.setProperty("ausphur", "no");
props.store(out, null);
out.close();
This will help!
Update:
There is no way to keep your comments back since Properties doesn't know about it. If you use java.util.Properties, it is a subclass of Hashtable which doesn't preserve the order of the keys and values the way they are inserted. What you can do is, you can go for LinkedHashMap collection with your own implementation of Properties datastore. But, you have to read the properties from file yourself and put it in LinkedHashMap. To Note, LinkedHashMap preserves the order in which keys are values are put. so, you can iterate the keyset and update the properties file in the same order back. Thus, the order can be preserved
You could use Properties Java class.
It allows you to handle in a very easy way properties files based on (key, value) pair.
Have a look at http://docs.oracle.com/javase/tutorial/essential/environment/properties.html
In particular:
Saving Properties
The following example writes out the application properties from the previous example using Properties.store. The >default properties don't need to be saved each time because they never change.
FileOutputStream out = new FileOutputStream("appProperties");
applicationProps.store(out, "---No Comment---");
out.close();
The store method needs a stream to write to, as well as a string that it uses as a comment at the top of the output.
Try this.
Properties prop = new Properties();
OutputStream output = null;
output = new FileOutputStream("config.properties");
// set the properties value
prop.setProperty("propertyname", "newValue");
prop.store(output, null);
Use java.util.Properties.
Properties props = new Properties();
FileInputStream fis = new FileInputStream("pathToYourFile");
props.load(fis);
fis.close();
props.setProperty("propName", "propValue");
FileOutputStream fos = new FileOutputStream("pathToYourFile");
props.store(fos, "Your comments");
fos.close();